I recorded my test in Selenium IDE; it runs fine in Firefox and Chrome. When I run it in IE 9, I keep getting this error. I am new to coding and am having trouble finding an answer to this. If anyone can explain to me in lay man's terms what is happening/why this may be the case, I'd appreciate it.
org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == BODY (WARNING: The server did not provide any stacktrace information)
Java Code:
assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Final[\\s\\S]*$"));
I would suggest printing out driver.getPageSource() before the call and check to see if the html that is on the current page matches your expectations.
If it does match what you'd expect then I'd print out the text content of all of the tags that match By.cssSelector("BODY") and see if any of them contain the text that you'd expect.
This will narrow down where the error is - you might need to find another way of finding this element.
Internally By uses XPath and casing matters with internet explorer.
Try using
assertTrue(driver.findElement(By.cssSelector("body")).getText().matches("^[\\s\\S]*Final[\\s\\S]*$"));
instead of
assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Final[\\s\\S]
*$"));
I believe that should work for you.
IE browser doesn't support Case-insensitive CSS attribute selectors. Try to use
driver.findElement(By.cssSelector("body"));
Related
I want to search something on Google using Selenium chromedriver and enter it. I can normally do this within the site, but I couldn't type it into google. What code can we use for this?
driver.findElement(By.xpath("//input[#class='desktopOldAutosuggestTheme-UyU36RyhCTcuRs_sXL9b']")).sendKeys("HBCV00000ODHHV");
Fakat olmadı.
The easiest way to do so is via the url when you perform driver.get(...).
Look at the google url for german shepards: https://www.google.com/search?q=german+shepards .
If you want to go the google.com and type in the search box you need to perform a better xpath query then by class name. As mentioned by #f1sh this is because google generates the class names (for scope based css). For me the following works for the search bar.
search_bar : WebElement = driver.get_element(By.XPATH, "//input[#title='Search']")
# then you can perform the send_keys
search_bar.send_keys(...)
Good Luck!
I think the problem is not the search text because I tried too many terms and it didn't work.
driver.findElement(By.xpath("//input[#class='desktopOldAutosuggestTheme-UyU36RyhCTcuRs_sXL9b']")).sendKeys("HBCV00000ODHHV");
driver.findElement(By.xpath("//input[#class='desktopOldAutosuggestTheme-UyU36RyhCTcuRs_sXL9b']")).sendKeys(Keys.ENTER);
these are just an example on different sites. I tried id class type as xpath in every way, but it didn't write the text I wrote in the search bar.
I have one doubt. Whenever I copy xpath from Firebug and try to use it in my selenium script, the functionality does not work. Or I get this error that unable to locate the element. But, certainly when I try to write expression and execute the same code snippet, it works fine. Why is it so, is there some problem with Firebug which is one of the popular tool.
suggestions welcomed.
There could be multiple reasons for the firebug generated XPath not to work in selenium.
Most common are two:
an element you are trying to find is not yet present in the DOM - this could happen when the page is not completely loaded, or is loaded asynchronously
there are iframe elements on a page. If you need to find and element inside an iframe, you need to switch to it first
Also, don't blindly trust the XPath generated by Firebug - most of the time it would not be the most reliable expression. If possible, operate id and class attributes and don't start your XPath expression from the root HTML element - make it relative (using //).
The issue here is not with the Xpath you are using,
First check where your element is located,
The reason here is your element is not visible to the driver,
The question here is why the element is not visible,
Work on this,
And Do provide the html code in your question so that it will be more helpful to resolve the issue quickly,
I would like to know how to identify via webdriver the following html "node":
thank you <em>very much indeed</em> - Angielsko-Polski Słownik <b>...</b>
(It's just any link of google when one launch a google search)
I have googled it, however I have found only cases where the id or the class were provided.
What about in this case?
This is my failing try:
webdriver.findElement(By.xpath("//a[#href='http://www.google.pl/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCcQFjAA&url=http%3A%2F%2Fpl.bab.la%2Fslownik%2Fangielski-polski%2Fthank-you-very-much-indeed&ei=Sia8U6LPCevB7AagwoCICg&usg=AFQjCNF6y7swYrp3axD0hNrCWfjovhcVPw&bvm=bv.70138588,d.bGE']")).click();
Thanks in advance.
There are several possibilities:
By.tagName("a")
However, chances are there are more than one a tag, and so the above will pick the first one it encounters. To get more specific, you can use:
By.xpath("//a[0]")
0 in this case refers explicitly to the first a tag. However, to give a precise XPath answer, I would need to see more your page code, as well as your exact requirements. You can also use:
By.partialLinkText("thank you very much indeed")
This works best if you have unique enclosed text.
You may also want to read through the rest of the locators in the API.
If you are not able to identify the the link directly, you can try based on other element.
When you have any adjacent div or element having unique value, you can refer the link relative to that.
WebElemenet element = driver.findElement(By.cssSelector("div#id a"));
This will get the link element which is present in the div having an id value of "id".
When I use firebug I get back this as the xpath it gives me back this /html/body/div[5]/div[2]/div/div[7]/div/div[4]/div/div[2]/div/ol/li/div/h3/a
I am unclear how to use this in Selenium Webdriver to click on a link.
Thanks!
This is well covered in the documentation.
http://docs.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-api-commands-and-operations
However, because its a very simple answer, you'd simply do something like:
driver.findElement(By.xpath("div[5]/div[2]/div/div[7]/div/div[4]/div/div[2]/div/ol/li/div/h3/a"));
(Assuming driver is a valid WebDriver instance and I've omitted the html/body section - it isn't needed).
Always try to simplify the xpaths, try using firebug with this xpath to see if it's unique, if not you'll need to be a little more specific.
"//h3/a"
Don't use xpath unnecessarily. It would make some problem in future. If there is no other way to locate that element as #Nora told try to simplify the xpath.
In your case you can use By.linkText,By.partialLinkText.
driver.findElement(By.linkText("linkName")).click();
driver.findElement(By.partialLinkText("partialTextOfLink")).click();
driver.findElement(By.xpath("//a[text()='LinkText']")).click(); //simplified xpath
You can use any of the above if there is no other attribute(id, name ..etc) available for that anchor tag.
i am trying get a element through selenium with code:
WebElement a = driver.findElement(By.xpath("//div[#id=':r6']/span/text()"));
using this same expression on a firefox plugin, the element is find , but in selenium(java code) this way the element is not found, someone can me help
The command that you might need is: "AllowNativeXPath" - then just use the Xpath (either via Xpather or after 'inspecting element')to identify your element. Sometimes, though ... there's still an issue where Selenium does not 'see' elements described with an Xpath while running a script, but when users click the 'Find' button ... Selenium has no trouble at all. I usually take the focus up a level and down a level before any commands that Selenium has trouble finding the elements for ... and it works well thereafter. It's ugly and very NOT elegant ... but it works.
Selenium uses it's OWN Xpath interpreter ... and the one native to your browser might be better in some cases.
You can try this instead:
WebElement a = driver.findElement(By.xpath("//div[#id=':r6']/span")).getText();