Webdriver 无法在 IE8 中找到元素
使用以下简化测试,无论我使用哪种 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 中运行没有问题。
Google 让我失望了,有谁知道为什么 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")
Attempting just print br.page_source 抛出一些编码错误,这就是打印行包含 unicode 编码内容的原因。不确定这在使用 IE 时是否符合预期,或者 utf-8 编码是否会妨碍我查找元素的尝试。
所以,我想真正的问题是:有人成功地在 64 位 Internet Explorer 中使用了 Webdriver Python API 吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 IE8 上一直无法解决这个问题。
然而,安装 IE9 后,可以按预期找到元素。
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?
这是一个 Java 示例,但您可以从中学习。
我发现一个好的故障排除技术是首先使用像这样的无异常调用:
然后,像这样执行 findElement:
这样,当未找到它时,它不会抛出异常。相反,您会收到人类可读的错误。
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:
Then, do findElement like this:
This way, when its not found, it doesn't throw an exception. Instead, you get a human readable error.