我使用Python 3.4和Selenium Webdriver python bindings。我在Windows机器上运行。 当我使用Selenium Chrome和Firefox网络驱动程序时,以下脚本可用于测试我的网站。但是,当我切换到IE webdriver时,它失败了。这是我的剧本:
driver = webdriver.Ie() # Line #1
appURL = ("http://localhost:3000")
driver.maximize_window()
driver.get(appURL)
print("Waiting for 'MyRadio' to be present")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,'MyRadioButtonID')))
print("'MyRadio' is present")
myRadioBtn = driver.find_element_by_id("MyRadioButtonID")
print("myRadioBtn = %s" % myRadioBtn)
print('myRadioBtn.get_attribute("disabled") = %s' % myRadioBtn.get_attribute("disabled"))
print('myRadioBtn.get_attribute("class") = %s' % myRadioBtn.get_attribute("class"))
print('myRadioBtn.get_attribute("data-name") = %s' % myRadioBtn.get_attribute("data-name"))
print('myRadioBtn.get_attribute("data-key") = %s' % myRadioBtn.get_attribute("data-key"))
print('myRadioBtn.get_attribute("name") = %s' % myRadioBtn.get_attribute("name"))
print('myRadioBtn.get_attribute("type") = %s' % myRadioBtn.get_attribute("type"))
print('myRadioBtn.is_enabled() = %s' % myRadioBtn.is_enabled())
print('myRadioBtn.is_displayed() = %s' % myRadioBtn.is_displayed())
print('dir(myRadioBtn) = %s' % dir(myRadioBtn))
print("\n")
print("About to click 'MyRadio'")
time.sleep(3)
myRadioBtn.click() # Line #28
print('myRadioBtn.get_attribute("value") = %s' % myRadioBtn.get_attribute("value"))
print("Clicked 'MyRadio' 1")
如上所述,这适用于Chrome和Firefox。但是,当我将第1行更改为"即#34;时,它在#28行中失败。我安装了IE 9。错误消息是"ElementNotVisibleException: Message: 'Cannot click on element'"
这种情况100%发生。以下是该失败产生的输出。
Waiting for 'MyRadio' to be present
'MyRadio' is present
myRadioBtn = <selenium.webdriver.remote.webelement.WebElement object at 0x0299F290>
myRadioBtn.get_attribute("disabled") = None
myRadioBtn.get_attribute("class") = myClass
myRadioBtn.get_attribute("data-name") = myDataName
myRadioBtn.get_attribute("data-key") = myKey
myRadioBtn.get_attribute("name") = myName
myRadioBtn.get_attribute("type") = radio
myRadioBtn.is_enabled() = True
myRadioBtn.is_displayed() = False
dir(myRadioBtn) = ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_execute', '_id', '_parent', '_upload', 'clear', 'click', 'find_element', 'find_element_by_class_name', 'find_element_by_css_selector', 'find_element_by_id', 'find_element_by_link_text', 'find_element_by_name', 'find_element_by_partial_link_text', 'find_element_by_tag_name', 'find_element_by_xpath', 'find_elements', 'find_elements_by_class_name', 'find_elements_by_css_selector', 'find_elements_by_id', 'find_elements_by_link_text', 'find_elements_by_name', 'find_elements_by_partial_link_text', 'find_elements_by_tag_name', 'find_elements_by_xpath', 'get_attribute', 'id', 'is_displayed', 'is_enabled', 'is_selected', 'location', 'location_once_scrolled_into_view', 'parent', 'rect', 'send_keys', 'size', 'submit', 'tag_name', 'text', 'value_of_css_property']
About to click 'MyRadio'
Traceback (most recent call last):
File "myFile.py", line 28, in <module>
myRadioBtn.click()
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py",line 65, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py",line 385, in _execute
return self._parent.execute(command, params)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: 'Cannot click on element'
为什么使用Firefox和Chrome驱动程序可以点击此单选按钮,而不是IE?我可以清楚地看到单选按钮是可见的。手动,我可以点击它。那么为什么不能硒?
答案 0 :(得分:2)
Selenium Internet Explorer Web驱动程序不是浏览器的本机驱动程序,并且通常在屏幕上查找元素时遇到问题。我怀疑这是你遇到的问题。
答案 1 :(得分:1)
首先,尝试将预期条件更改为visibility_of_element_located
:
期望检查元素是否存在于DOM的DOM上 页面和可见。可见性意味着元素不仅仅是 显示但高度和宽度都大于0。
请注意,您不需要再次找到该按钮,它会返回网络元素:
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'MyRadioButtonID')))
element.click()
此外,对于IE,设置较高的implicit wait timeout可能会产生积极的影响:
driver.implicitly_wait(10) # seconds
答案 2 :(得分:0)
您应该尝试禁用浏览器本机事件。感谢Jim Evans如此广泛地解释Native event
profile = driver = webdriver.Ie()
profile.native_events_enabled = False
driver = webdriver.Ie(profile)
答案 3 :(得分:0)
您使用的Locators值必须在IE中更改,因为它发生IE的行为与FF和Chrome不同,尝试使用指定给IE的定位器或使用IE可见的定位器