开发者

Webdriver fails to find elements in IE8

开发者 https://www.devze.com 2023-03-28 18:51 出处:网络
Using the following simplified test, webdriver fails to find the intended 开发者_开发问答element no matter what sort of find_by call I use.

Using the following simplified test, webdriver fails to find the intended 开发者_开发问答element no matter what sort of find_by call I use.

import unittest
from selenium import webdriver

class Setups(unittest.TestCase):
    def setUp(self):
        self.browser = webdriver.Ie()
        self.browser.implicitly_wait(15)

    def tearDown(self):
        self.browser.quit()

class IETest(Setups):
    def test_ietest(self):
        br = self.browser
        br.get("http://www.gmail.com")
        br.find_element_by_id("Email").send_keys("anything")

if __name__ == '__main__':
    unittest.main(verbosity=2)

After the implicit wait times out trying to find an element with id == Email the following error message appears in the output:

test_ietest (__main__.IETest) ... ERROR

======================================================================
ERROR: test_ietest (__main__.IETest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "V:\Automation\Tests\web_ietest.py", line 23, in test_ietest
    br.find_element_by_id("Email").send_keys("anything")
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 172, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 525, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 144, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 118, in check_response
    raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to find element with id == Email'

----------------------------------------------------------------------
Ran 1 test in 19.707s

FAILED (errors=1)

Attempts to run the same test using find_by_xpath and find_by_css_selector returns the same result. Of course, the same example test runs without issue in Firefox.

Google has failed me, does anyone have any insight as to why IE refuses to locate page elements that are clearly present?

Technical info:

  • Windows 7 64-bit
  • Selenium 2.4.0
  • Python 2.7.1
  • Internet Explorer 8.0.7601.17514 64-bit

Some additional investigation proves that the page source is fully loaded and correct before the find_element is attempted. Adding the following line between the get(url) and the find_element_by_id call prints the page source and the element with an id of "Email" is present:

print unicode(br.page_source).encode("utf-8")

Attempting just print br.page_source throws some encoding error which is why the print line includes the unicode encoding stuff. Not sure if this is as expected when working with IE, or if the utf-8 encoding could be hampering my attempts to find elements.


So, I guess the real question is this: Has anyone successfully used the Webdriver Python API with 64bit Internet Explorer?


I was never able to solve this issue on IE8.

After installing IE9, however, elements were able to be found as expected.


Did you try increasing the implicitly wait timeout?


This is a Java example but you can learn from it.

I find that a good troubleshooting technique is to first use a exceptionless call like this one:

Set<WebElement> found = driver.findElements( By.cssSelector("some selector") );

Then, do findElement like this:

if ( found.size() == 0 ) {
    Assert.assertTrue("Found zero x", found.size() == 1 );
} else if ( found.size() == 1 ) {
    WebElement x = found.iterable().next();
} else {
    Assert.assertTrue("Error looking for x", found.size() >= 1 );
}

This way, when its not found, it doesn't throw an exception. Instead, you get a human readable error.

0

精彩评论

暂无评论...
验证码 换一张
取 消