Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

CSS Selector raises WebDriverException instead of NoSuchElementException when element not found #3470

Closed
lukeis opened this issue Mar 3, 2016 · 15 comments

Comments

@lukeis
Copy link
Member

lukeis commented Mar 3, 2016

Originally reported on Google Code with ID 3470

What steps will reproduce the problem?
1. use a CSS Selector to check that an element is NOT present

What is the expected output? What do you see instead?
This should raise a NoSuchElementException as for By.id and By.xpath.
Instead it raises a WebDriverException.

Selenium version: 2.19.0
OS: Windows7
Browser: Firefox and IE
Browser version: 10.0.2 & 8


Additional information:

I wrap the test in a function like this so I can pass whichever By type I like. I've
had to add an extra error trap for the WebDriverException to avoid a crash but I don't
like it as it could trap a genuine exception too.

    public static boolean isElementPresent(By by)
    {
        try
        {
            dr.findElement(by);
            System.out.println("PASS: Element found: " + by.toString());
            return true;
        }
        catch (org.openqa.selenium.NoSuchElementException nse) 
        {
            System.out.println(nse.getMessage());
            return false;
        }
        catch (org.openqa.selenium.WebDriverException wde)
        {
            System.out.println(wde.getMessage());
            return false;            
        }
    }

Reported by richard.dickinson@ca.com on 2012-03-01 13:02:20

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Reported by dawagner on 2012-04-10 23:24:21

  • Labels added: GettingInvolved

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

A stacktrace showing the issue on Windows XP SP3, Selenium 2.21.0, Internet Explorer
8, searching for an element by XPath (so no CSS selector as OP stated):

Error: org.openqa.selenium.WebDriverException (Unable to find element with xpath ==
//*[contains(text(),'Align') or contains(@value,'Align')] (WARNING: The server did
not provide any stacktrace information)
Command duration or timeout: 688 milliseconds
Build info: version: '2.2.1', revision: '16551', time: '2012-04-11 21:42:35'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version:
'1.6.0_22'
Driver info: driver.version: RemoteWebDriver)

sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:175)
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:128)
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:459)
org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:227)
org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:312)
org.openqa.selenium.By$ByXPath.findElement(By.java:344)
org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:219)

Reported by PhotoSlanec on 2012-05-02 19:40:17

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Please provide a reduced test case (WebDriver code with accompanying HTML demo page)
to reproduce the issue.

This is not a common issue, it can be related to your page or the locator you use.
Here is an example that shows that selenium throws a valid exception:

RemoteWebDriver driver = new InternetExplorerDriver();
driver.get("http://localhost/");
driver.findElement(By.cssSelector("test"));

--->

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find
element with css selector == test (WARNING: The server did not provide any
 stacktrace information)
Command duration or timeout: 304 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.19.0', revision: '15849', time: '2012-02-08 16:10:57'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version:
'1.6.0_30'
Driver info: driver.version: RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:170)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:123)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:439)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:226)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:291)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:407)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:218)
    at org.openqa.selenium.example.Sample.main(Sample.java:50)

Reported by barancev on 2012-05-11 14:03:42

  • Status changed: NeedsClarification
  • Labels added: Component-WebDriver, Lang-Java

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

That's the pickle - it happens only sometimes (but at the same place, that's true) and
I can't get my hands around it. I'll try to reduce the test case in the upcoming days.
Thanks for looking at it, anyway!
(Also, could you please rename the topic header, as I proved that it happens with XPath
selectors, too?)

To the OP: What did precede the failing call? In our case, it usually is async redirect
that hasn't completed yet...

Reported by PhotoSlanec on 2012-05-11 14:27:38

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

I also managed to reproduce with an xpath selector against the same element as I originally
saw the problem with the css selector some time ago.
I'm testing a proprietary system and can't just paste the code I'm afraid and it isn't
easy to just isolate the offending element. 

Reported by richard.dickinson@ca.com on 2012-05-11 14:32:56

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

I've faced the same problem with IE 9. All tests were executed, browser redirected,
but all of them failed because of the same Exception as is mentioned here (org.openqa.selenium.WebDriverException
(Unable to find element with xpath == //*[contains(text(),'some text')). The problem
was, that JavaScript was disabled in browser. If you are using IE 9 (don't know if
developers tools are in previous versions too), try to press F12 and check if in the
pop-up developer tools window is unchecked option Disabled > Script.

Reported by jstefek%redhat.com@gtempaccount.com on 2012-06-11 11:25:53

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

I'm facing the same problem with IE 8. Please find the details below:
Build info: version: '2.24.1', revision: '17205', time: '2012-06-19 17:28:14'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version:
'1.6.0_25'
Driver info: driver.version: RemoteWebDriver

The same code is performing well in a different system with same configuration.

Reported by abhishekganguly85 on 2012-07-23 12:47:59

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

What we really need is a reproduction case, not configuration details.

I've provided a sample that shows this feature works *in general* (see comment 3).

Obviousely there is an edge case where it does not work, and we need to catch it.

Reported by barancev on 2012-07-23 13:23:56

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

The attached testcase reproduces the issue.

This also reproduces bug http://code.google.com/p/selenium/issues/detail?id=3800 which
is the same issue with the difference of using findElement instead of findElements


The issue seems to be that the current page changes while findElement(s) is running.

Reported by morgan.christiansson on 2012-09-03 14:38:56


- _Attachment: [Test.java](https://storage.googleapis.com/google-code-attachments/selenium/issue-3470/comment-10/Test.java)_ - _Attachment: [test1.html](https://storage.googleapis.com/google-code-attachments/selenium/issue-3470/comment-10/test1.html)_ - _Attachment: [iedriver.log](https://storage.googleapis.com/google-code-attachments/selenium/issue-3470/comment-10/iedriver.log)_

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

I'm also getting JavaScript errors in the JS console when this happens:


Message: Can't execute code from a freed script
Line: 1
Char: 72
Code: 0
URI: http://xx.xx.xx.xx/xxxx

Reported by morgan.christiansson on 2012-09-03 14:42:41

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Since bug 3800 is (hopefully) fixed, is this fixed, too? I can't reproduce the bug via
the testcase in comment #10 (though I haven't really been able to reproduce it with
an old version, either).

Reported by PhotoSlanec on 2013-02-20 12:08:20

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Hi!

At this moment at my machine (Win7 x64) i see:
* 38 errors with incorrect exception types for local drivers (4 from 5, Safari is not
supported yet)
* 44 errors with incorrect exception types for remote drivers (5 from 5)

Error means that type of raised exception is out of list (NoSuchElementException, NoSuchWindowException,
InvalidSelectorException).
This list is made from WD protocol (with one fix - XpathInvalidError replaced by InvalidSelectorException).

NOTE: this is for python binding. I think for another bindings list of errors will
be different.
It would be great if someone create tests for each binding using my scripts as examples.
:)

Selenium version is 2.31. Chromedriver and IEDriverServer are last from Downloads.
Browsers list:
* IE 9.0.xxx x32
* FF 19.0.2
* Chrome 25.0.xxx
* Opera 12.14
* Safari 5.1.7

The found errors are attached as 2 files: remote_errors and local_errors. 2 test scripts
are attached too.

Format of each error (2 variants for single element and multiple elements differs only
by S):

<driver id>: Non correct exception <exception type>()(<exception message>) is occured
during search element by: (<selector type>, <selector value>)
<driver id>: Non correct exception <exception type>()(<exception message>) is occured
during search elements by: (<selector type>, <selector value>)

Reported by a.u.savchuk on 2013-03-24 12:13:46


- _Attachment: [remote_test.py](https://storage.googleapis.com/google-code-attachments/selenium/issue-3470/comment-13/remote_test.py)_ - _Attachment: [local_test.py](https://storage.googleapis.com/google-code-attachments/selenium/issue-3470/comment-13/local_test.py)_ - _Attachment: [remote_errors.txt](https://storage.googleapis.com/google-code-attachments/selenium/issue-3470/comment-13/remote_errors.txt)_ - _Attachment: [local_errors.txt](https://storage.googleapis.com/google-code-attachments/selenium/issue-3470/comment-13/local_errors.txt)_

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Reported by barancev on 2013-10-29 23:05:49

  • Status changed: New

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

IE and Firefox are green in the trunk.

Opera (deprecated) and Chrome have failing tests, see @Ignore annotations in ElementFindingTest

Reported by barancev on 2014-01-02 10:10:27

  • Status changed: Fixed
  • Labels removed: GettingInvolved

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Reported by luke.semerau on 2015-09-17 18:14:55

  • Labels added: Restrict-AddIssueComment-Commit

@lukeis lukeis closed this as completed Mar 3, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant