【问题标题】:Why can Selenium click on radio button using Chrome & Firefox but fails with IE 9?为什么 Selenium 可以使用 Chrome 和 Firefox 单击单选按钮,但在 IE 9 中失败?
【发布时间】: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


【解决方案1】:

您使用的定位器值必须在 IE 中更改,因为 IE 的行为与 FF 和 Chrome 不同,请尝试使用指定给 IE 或使用 IE 可见的定位器

【讨论】:

    【解决方案2】:

    Selenium Internet Explorer Web 驱动程序不是浏览器的原生驱动程序,并且经常在屏幕上定位元素时出现问题。我怀疑这是您遇到的问题。

    【讨论】:

    【解决方案3】:

    您应该尝试禁用浏览器原生事件。感谢 Jim Evans 如此广泛地解释 Native event

    profile = driver = webdriver.Ie() 
    profile.native_events_enabled = False
    driver = webdriver.Ie(profile)
    

    【讨论】:

      【解决方案4】:

      首先,尝试将预期条件更改为visibility_of_element_located

      检查一个元素是否存在于一个元素的 DOM 上的期望 页和可见。可见性意味着元素不仅 显示,但也有一个大于 0 的高度和宽度。

      请注意,您不需要再次找到该按钮,它会返回 web 元素:

      element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'MyRadioButtonID')))
      element.click()
      

      另外,对于 IE,设置较高的implicit wait timeout 可能会产生积极影响:

      driver.implicitly_wait(10) # seconds
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-19
        • 2010-11-03
        • 2014-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-11
        • 2014-02-14
        相关资源
        最近更新 更多