So I am just beginning to learn C#, and I am currently working with selenium. I have used selenium with Python without issues, however I am having trouble finding an element's XPath with C#.
The issue arises when trying to declare the element in the first place. When I use:
driver.FindElementByXPath();
I paste the XPath from the button, however C# gives me an error immediately; it looks like it is having trouble understanding the XPath, and I would assume it is because of the quotations.
I have attached an image below. I understand this may be a stupid issue with an easy fix, but all help appreciated, thanks!
You have to use single quotes marks inside double quotation marks.
var sizeElement = chromeDriver.FindElementByxPath("//*[#id='oCartChoose']/button[3]");
Or vice versa.
var sizeElement = chromeDriver.FindElementByxPath('//*[#id="oCartChoose"]/button[3]');
Related
Does anybody know how to filter a column of excel using the INTEROP C#, and filter all the values that contains 'ODM', or 'AAS'. I've already tried to use the namedRange.autofilter but it returns just those VALUES that is write ONLY 'ODM' or 'AAS' and nothing else.
This link seems to indicate Excel filters do not support regex. My own experience resonates with that. So, taking a step back, how can we solve your problem? Well, looking at what the VBA guys did, the idea seems to be as follows:
Read the entire column into an array: Interop lets you do this in one efficient swoop
Use C#'s powerful Regex engine to filter that array down to a set of values conforming to your regex
Call the Autofilter function with Criteria1 dynamically generated as the array you just created using the regex filter in the last step.
I'm not going to code that for you, but I'm quite certain the above approach will work and am happy to consult with you if you need assistance on it.
I am using Selenium with C# to run some tests. I have an issue with an element which i can not locate. I know it is there, and I have it in html. even knowing everything about it, i can not seem to find it. When I used nodeJS it was pretty easy to locate, but in C# I just can not. After trying so many things, I thought I would ask here.
I get OpenQA.Selenium.NoSuchElementException when using the following:
IWebElement Title = driver.FindElement(By.XPath("//*[contains(., 'TextHere')]"));
I tried using the css path, xpath and tag, but the only case where I even found something was with tag. And it was not the correct element.
Is there just something majorly wrong with my syntax? I have looked at multiple threads about this and all the syntax they used haven't worked either.
say or between two calls of contains function
//*[contains(text(), 'About us') or contains(text(), 'about us')]
or use translate function to make xpath case insensitive
//*[contains(translate(text(), 'ABOUTS', 'abouts'), 'about us')]
when you are using Contain text make sure the contain text is unique or else you will get list of result and based on your condition iterate it.
Ok, so I figured it out. The element was inside an iframe. But not only that, it was also being created by a function which ran on load. Meaning i had to put in a delay to wait for it to load.
Here is the working code in case anyone is looking at a similar issue:
driver.SwitchTo().Frame(driver.FindElement(By.Id("iFrame id")));
System.Threading.Thread.Sleep(500);
IWebElement Title = driver.FindElement(By.XPath("path to element"));
Just using sleep might not be optimal, but I am quite new at this, and it works. If someone has a good replacement i would love to hear suggestions.
I've been trying to figure this out for quite a while now and I seem to be getting nowhere.
I've been trying to display special chars on a webpage but I'm having lots of trouble achieving my goal.
I'll use the † symbol as an example to explain my issue.
I'm loading the string from a database so the symbol is stored as \u0086
Let's use the word †est as an example.. When I load the string on CodeBehind I get "\u0086est" and on the webpage I get est instead of the correct symbol.
I've been trying to encode the string in multiple ways but it seems that I'm out of luck as I can't seem to get it to work.
The closest I got was using this:
System.Text.Encoding.GetEncoding(1252).GetString(HttpUtility.UrlDecodeToBytes(myString))
which returned †est ... However I'm not certain if it would be a good idea to specify the codepage like this as I'm loading the strings from a database and other special characters may appear as well.
It would be great if I could get some help.
EDIT:
I'm simply setting a label's text to the string I'm loading from the database.
myLabel.InnerText = myString;
I'm having what seems like a really simple problem. I'm trying to navigate to an element in HTML by Xpath, and can't seem to get it to function properly.
I want to grab a span from the html contents of a page. The page is fairly complex, so I've been using Firebug's "get element by xpath" and pasting the result into my code. I've noticed it's slightly different than the xpath you get from doing the same thing in Chrome, but they both seem to direct to the same place.
The html I'm trying to navigate is found here. The field I'm trying to access via xpath is the first "Results 1 - 10 of n".
Based on FireBug's 'inspect element' the xpath should be: /html/body/div/center/table/tbody/tr[6]/td/table/tbody/tr/td[2]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/span
However when I try to use this xpath to identify the element in a C# codebehind, it gives me a number of errors that that path cannot be found.
Am I doing something wrong here? I've tried a number of permutations of the xpath and I don't understand why this wouldn't be cooperating within code.
Edit: I'm having this problem both in HTMLAgilityPack (but managed to hack out a bad solution using regexes instead) and a SELECT statement modeled after the answer found here
Edit 2: I'm trying to figure out this issue using Yahoo's free proxy as shown in the example here:
var query = 'SELECT * FROM html WHERE url="http://mattgemmell.com/2008/12/08/what-have-you-tried/" and xpath="//h1" and class="entry-title"';
var url = "http://query.yahooapis.com/v1/public/yql?q=" + query + "&format=json&callback=??";
$.getJSON(url,function(data){
alert(data.query.results.h1.content);
})
I'm having the same problems with HTML agility pack but I'm more interested in getting this part to work. It works for the provided URL that the answerer gave me (seen above). However when I try to use even simple xpath expressions on the http://nl.newsbank.com url, I get errors that no object has been retrieved every time, no matter how basic the xpath.
Edit 3: I thought I'd elaborate a little more on the big picture of the larger problem I'm trying to solve of which this problem is a critical component in the hopes that maybe it provides a little more insight.
To learn basic ASP.NET development skills from scratch, I decided to make a simple web application, based around the news archive search at http://nl.newsbank.com/. In its current iteration, it sends a POST request (although I've now learned you can use a GET request and just dump the body at the end of the URL) to send search criteria, as if the user entered criteria in the search bar. It then searches the response (using RegExes, not Xpath because I couldnt get that working) for the "Results 1-n of n" span, extracts n, and dumps it in a table. It's a cool little tool for looking up news occurrence rates over time.
I wanted to add functionality such that you could enter a date range (say May 2002 - June 2010) and run a frequency search for every month / week in that range. This is very easy to implement conceptually. HOWEVER the problem is, right now all this happens server side, and since there's no API, the HTTP response contains the entire page, and is therefore very bandwidth intensive. Sending dozens of queries at once would swallow absolutely unspeakable amounts of bandwidth and wouldn't be even a little scalable.
As a result I tried rewriting the application to work client-side. However because of the same-origin policy I'm not able to send a request to an external host from the client-side. HOWEVER there is a loophole that I can use a free Yahoo proxy that makes the request and converts it to JSON, and then I can use the JSON exception of the Same-Origin Policy to retrieve that data from the proxy.
Here's where I'm running into these xpath problems specific to http://nl.newsbank.com. I'm not able to retrieve html with any xpath, and I'm not sure why or how I can fix it. When debugging in VS2010, I'll receive the error Microsoft JScript runtime error: Unable to get value of the property 'content': object is null or undefined
As paul t. already mentioned in a comment, the TBODY elements are generated by the webkit engine. The next problem is that the DIV between the BODY and CENTER does not exist on the page by default. It is added by an JS statement on line 119.
After stripping out the DIV and TBODY elements like
/html/body/center/table/tr[6]/td/table/tr/td[2]/table/tr/td/table/tr/td/table/tr/td/table/tr/td/span
i can successfull select a node with the HthmlAgilityPack.
Edit: don't use tools like Firebug for getting an XPath value on a website. Don't even use it if you just want wo look at the source of the page. The problem with Firebug is, that it will show you the current DOM document tree which probably on almost every is already (heavily) modified by JS.
Your sample HTML page's elements haven't got many classes to select on, but if you're interested in the first <span> element that contains "Results: 1 - 10 of n", you can use an XPath expression that explicitly targets this textual content.
For example:
//table//span[starts-with(., "Results:")]
will select all <span> elements, contained in a <table>, and that contain text beginning with "Results:" (the //table is not strictly necessary in your case I think, but might as well restrict a little)
You want the first one of these <span>, so you can use this expression:
(//table//span[starts-with(., "Results:")])[1]
Note the brackets around the whole previous expression and then [1] to select the first of all the <span> matching the text
It may sound kind of simplistic, but the element you are looking for is the only doc element that is using the css class "basic-text-white". I would think this would be a lot easier to find and extract than a long xpath. Web-scraping is never a stable thing, but I would think this is probably as stable as the xpath. Trying to debug the xpath just about makes my eyes bleed.
I have researched this for a few hours and I am kind of frustrated. Maybe I am just missing something as I am new to blogging.
I am not hosting my own blog, I am just using WordPress.com.
I want to include snippets of c# code and have them look like they do in Visual Studio, or at least make them look nice, certainly with line numbers and color.
The solutions I have seen for this all seem to assume you are hosting your own blog.
I cannot figure out how to install plugins.
Is there a widget that will make code snippets look nice, or some other solution I can easily use?
Thank you
EDIT: Sarfraz has outlined one way to solve my problem (thank you!), and I have tried it but there is an issue I have, namely that it does not colorize most of my code (newer keywords like var, from, where, select, etc). Is there a fix to this or is there some other solution?
Just edit your aricles in html mode and enclose your code within these tags.
[sourcecode language="css"]
[/sourcecode]
Example:
[sourcecode language="javascript"]
// javascript hello world program
alert('Hello, World !!');
[/sourcecode]
Note: You need to specify correct language identifier for the language attribute as shown above.
More Information Here :)
The [sourcecode] tag usually works fine for C#, but for me it often breaks when I post XAML code.
Instead I use this page to format my code. The result looks nice (you can see it on my blog), but it requires the "Custom CSS" option ($15/year).
EDIT: actually the [sourcecode] tag works fine, and I'm now using it in all my posts
[code language="csharp"]
//Your code here
[/code]
looks like this has been updated, now you can use
[code language="[the lang you are posting]"]
your code here
[/code]
note: you can shorthand language as lang
[code lang="[the lang you are posting]"]
your code here
[/code]
here is the list of supported languages