2011-08-16 75 views
0

使用以下简化测试,无论使用哪种find_by调用,webdriver都无法找到目标元素。Webdriver无法在IE8中找到元素

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 ==电子邮件出现在输出以下错误消息:

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位
  • 硒2.4.0
  • 的Python 2.7.1
  • 的Internet Explorer 8.0.7601.17514 64位

一些额外的调查证明,页面源是完全loade d并在尝试find_element之前更正。添加和GET(URL)之间以下行find_element_by_id调用打印页面的源代码,并用“电子邮件”的ID的元素存在:

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

试图只是打印br.page_source抛出一些编码错误,这就是为什么打印行包括unicode编码的东西。不知道这是否与IE一样符合预期,或者utf-8编码会妨碍我寻找元素的尝试。


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

回答

2

我从来没有能够解决这个问题在IE8上。

然而,在安装IE9之后,能够按预期找到元素。

0

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

+0

增加隐式等待,甚至高达200,返回相同的结果。该页面明显完成加载,但find_element只是找不到该元素。 – Tfill

+0

这适用于IE8,WinXP,Selenium 2.2,Java。我所做的只是WebDriver driver = new InternetExplorerDriver(); driver.get(“http://www.gmail.com”); (“电子邮件”))。sendKeys(“hello”); – nilesh

+0

注意到http在评论中被删除了... – nilesh

0

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

我发现一个很好的故障排除方法是先用exceptionless调用像这样的:

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

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