invalidselector exception + selenium - xpath

I have been hit with InvalidSelectorException on a regular basis when i run my selenium web driver test script
My test fails with this InvalidSelector Exception and throws message as :
The xpath expression '//form[#name=somelocator]' cannot be evaluated or does not result in a WebElement
The element locator i use is perfect which return a correct web element when i cross verify it using xpath checker / or with browser console, so no issue with the element locator
I click on some button and then wait for the above xpath locator element to load in the application UI and giving enough waitTilllElementVisible() methods before checking the above locator
I found that many faced this issue, but could not get the concrete reason for this failure.
Selenium asks to visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html for the documentation on this error, but this link looks not up
Reproduced it with selenium 2.45 and 2.46 version with IE11
Please let me know if you knows the reason for this and why it throws invalidSelector exception on browser open with webdriver

I think the problem is the SPACE character in the #name tag. Try "//form[#name='some locator']" or '//form[#name=\'some locator\']'.

#peetya's answer is probably correct and a better answer but I prefer CSS Selectors... they are less error prone if you have the option to not use XPath.
driver.findElement(By.cssSelector("form[name='some locator']"));
CSS Selector reference

Related

How to detect when Selenium loads a browser's error page

Is there a universal way to detect when a selenium browser opens an error page? For example, disable your internet connection and do
driver.get("http://google.com")
In Firefox, Selenium will load the 'Try Again' error page containing text like "Firefox can't establish a connection to the server at www.google.com." Selenium will NOT throw any errors.
Is there a browser-independent way to detect these cases? For firefox (python), I can do
if "errorPageContainer" in [ elem.get_attribute("id") for elem in driver.find_elements_by_css_selector("body > div") ]
But (1) this seems like computational overkill (see next point below) and (2) I must create custom code for every browser.
If you disable your internet and use htmlunit as the browser you will get a page with the following html
<html>
<head></head>
<body>Unknown host</body>
</html>
How can I detect this without doing
if driver.find_element_by_css_selector("body").text == "Unknown host"
It seems like this would be very expensive to check on every single page load since there would usually be a ton of text in the body.
Bonus points if you also know of a way to detect the type of load problem, for example no internet connection, unreachable host, etc.
WebDriver API doesnot expose HTTP status codes , so if you want to detect/manage HTTP errors, you should use a debugging proxy.
See Jim's excellent post Implementing WebDriver HTTP Status on how to do exactly that.
If you just need to remote-control the Tor Browser, you might also consider the Marionette framework by Mozilla. Bonus: It fails when a page cannot be loaded: (see navigate(url) in the API)
The command will return with a failure if there is an error loading
the document or the URL is blocked. This can occur if it fails to
reach the host, the URL is malformed, the page is restricted (about:*
pages), or if there is a certificate issue to name some examples.
Example use (copy from other answer):
To use with the Tor Browser, enable marionette at startup via
Browser/firefox -marionette
(inside the bundle). Then, you can connect via
from marionette import Marionette
client = Marionette('localhost', port=2828);
client.start_session()
and load a new page for example via
url='http://mozilla.org'
client.navigate(url);
For more examples, there is a tutorial.

Selenium testing with DoubleClick AD on page

I am currently writing a selenium test case for a page which has doubleClick ad integrated in header and footer.
Selenium works perfectly fine until it comes to test this page, and test breaks with following issue.
Exception class=[java.lang.IllegalArgumentException]
com.gargoylesoftware.htmlunit.ScriptException: Exception invoking Window.getComputedStyle() with arguments [Text, String]
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:669)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:601)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
I wonder any one has gone through issue.
Here is my stack
WebDrive : Firefox
HtmlUnit 2.12
Selenium-htmlunit-driver-2.33.0
I think css getting pulled by doubleclick on runtime causing the issue.
Any help in this regard will be helpful.

When testing with WatiN: "'DebuggerDisplayProxy()' threw an exception of type 'System.InvalidCastException'"

FrameworkActionsLink = CurrentBuyerSite.BuyerWorkAreaFrame.Link(Find.ById("actions"));
I have code which finds a link element by finding by ID.
The HTML on the page is:
<a id="actions" href="Javascript:ShowMenuItem('options','actions'); ... >
I have excluded the full Javascript code in the href where you see "...".
On test execution, sometimes this code works correctly, but sometimes I get the following error:
'DebuggerDisplayProxy()' threw an exception of type
'System.InvalidCastException'
It's a pretty straightforward bit of code, I just wondered if anyone had experienced similar problems or error messages. Do you think this is an issue with the WatiN tool, the web application, the internet browser, or perhaps something else entirely?
Can you try the following code:
FrameworkActionsLink = (WatiN.Core.Link)CurrentBuyerSite.BuyerWorkAreaFrame.Link(Find.ById("actions"));
I'm pretty sure that it doesn't like the link being JavaScript:...
Edit: Try maybe this:
CurrentBuyerSite.Eval("ShowMenuItem('options','actions'); ...");
What runner are you using? if nUnit 64bit then try 32bit or other runner.
Maybe the issue is that page didn't fully download and that makes problem with javascript function, try replace Click() for ClickNoWait().

Selenium RC test passes when it should fail on verifyTrue(selenium.isTextPresent("string"))

I have a suite of Selenium tests that I created in the Selenium IDE and ported over to Java. In several tests I use the Java equivalent of the verifyTextPresent command to confirm some text on the page (verifyTrue(selenium.isTextPresent())).
I found an spelling error in the text on the page when running the test from the IDE, but the error was not caught when running the test via Selenium RC/TestNG. Here's an example of the code that I have and the text that is causing the problem (spelling error in bold):
Text:
Please correct the errors indicated below.
You need to add a least one restriction.
IDE:
verifyTextPresent | Please correct the errors indicated below.
verifyTextPresent | You need to add at least one restriction.
Java:
verifyTrue(selenium.isTextPresent("Please correct the errors indicated below."));
verifyTrue(selenium.isTextPresent("You need to add at least one restriction."));
Since both versions of the test have the correct text, why is the Selenium RC version not catching the error? Has anyone else had this problem?
The reason why this happens is that the test continues to run after the call to verifyTrue(). Verifications in Selenium catch the exceptions that would be thrown by a failed verification, as opposed to an assertion which throws an exception and causes the test to fail. Because verifications catch exceptions instead of throwing them, the test passes.
At the end of the test, the method checkForVerificationErrors() needs to be called to see if any of the verifications failed. If the method is not called, any verification errors will be ignored and the test will still pass (absent any other problems).
A discussion on the Selenium Google Group about the error itself is here. A discussion about the various verifications vs. assertions is here.
I had a similar problem... My workaround is to use assertTrue() instead of verifyTrue(). I hope it works for you to.

breakdown xpath

I am looking at the website. Trying to transfer selenium html to junit but could not get it to work because it keeps saying Error: Element not found. Maybe syntax error because I was able to break it down to the shortest path in firebug but still could not get to compile..What do you do in this case ?
Enrollment
I use firebug Xpath to get the value of the above link
/html/body/div[#id='contentDisplayPane']/div[#id='mainDiv']/div[#id='mainDivContent']/div[#id='simpleBox']/table/tbody/tr[2]/td[#id='fb_PageContent']/table/tbody/tr/td/table/tbody/tr/td[4]/a
Using firebug xpath, I was able to break it down to this and able to access Enrollment link..However when I put this in the junit test case selenium.click(("//div[#id='simpleBox']/table/tbody/tr[2]/td[#id='fb_PageContent']/table/tbody/tr/td/table/tbody/tr/td[4]/a");
I get ERROR: Element //div[#id='simpleBox']/table/tbody/tr[2]/td[#id='fb_PageContent']/table/tbody/tr/td/table/tbody/tr/td[4]/a") not found
Any help or tip is appreciated
Hey If you want to click any Link on your web page.
You have to first find Xpath of this Element/Text, For this you can use Firebug.So here an ex: You can use in IDE selenium.click(//a[text()="ON"]) and convert it to junit and get this
selenium.click("//a[text()=\"HereYouCanPutYourText\"]");
please feel free if you have any concern..
Are the tbody elements actually in the html or just being inserted by Firefox? If Firefox is inserting the tbody and your junit tests are driving a different browser, the tbody might not be there. Just a shot in the dark since you didn't post the document you're testing, but maybe it'll help.

Resources