Webdriver无法在IE8中找到元素

时间:2011-08-16 21:13:38

标签: python internet-explorer-8 webdriver selenium-webdriver

使用以下简化测试,无论我使用何种find_by调用,webdriver都无法找到目标元素。

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)

在尝试查找具有id == Email的元素的隐式等待超时后,输出中会出现以下错误消息:

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)

尝试使用find_by_xpath和find_by_css_selector运行相同的测试会返回相同的结果。当然,相同的示例测试在Firefox中没有问题。

谷歌让我失望,有没有人知道为什么IE拒绝找到明显存在的页面元素?

技术信息:

  • Windows 7 64位
  • Selenium 2.4.0
  • Python 2.7.1
  • Internet Explorer 8.0.7601.17514 64位

一些额外的调查证明,在尝试使用find_element之前,页面源已完全加载并且正确。在 get(url) find_element_by_id 调用之间添加以下行会打印页面源,并且ID为“Email”的元素存在:

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

尝试 print br.page_source 会引发一些编码错误,这就是打印行包含unicode编码内容的原因。使用IE时不确定这是否符合预期,或者utf-8编码是否会妨碍我查找元素的尝试。


所以,我想真正的问题是:有没有人成功使用64位Internet Explorer的Webdriver Python API?

3 个答案:

答案 0 :(得分:2)

我无法在IE8上解决这个问题。

安装IE9后,可以按预期找到元素。

答案 1 :(得分:0)

您是否尝试过增加隐式等待超时?

答案 2 :(得分:0)

这是一个Java示例,但您可以从中学习。

我发现一个好的故障排除技术是首先使用像这样的无异常调用:

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

然后,像这样做findElement:

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 );
}

这样,当找不到它时,它不会抛出异常。相反,你会得到一个人类可读的错误。