【发布时间】:2014-12-13 04:53:37
【问题描述】:
我正在使用带有Selenium Webdriver python bindings 的 Python 3.4。我在 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 行更改为“Ie”时,它在第 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 则不能?我可以清楚地看到单选按钮是可见的。手动,我可以点击它。那么为什么硒不能呢?
【问题讨论】:
-
这种情况时有发生。在我看来,有两种可能的解决方案,1:降级您的 IE 版本或 2:使用 selenium 执行更“粗略”的坐标点击,而不是点击按钮。
-
很遗憾,我无法更改我的 IE 版本。我需要在这个特定版本的 IE 上做实验。
-
你可以尝试使用 JavascriptExecutor 点击元素吗?
标签: python selenium python-3.x selenium-webdriver automation