为什么Selenium在Chrome和Firefox中能点击单选按钮,但在IE 9中失败?
我正在使用Python 3.4和Selenium Webdriver的Python绑定。我的电脑是Windows系统。下面这个脚本可以在我使用Selenium的Chrome和Firefox浏览器时正常测试我的网站。但是,当我换成IE浏览器的驱动时,它就不行了。以下是我的脚本:
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: '无法点击元素'"
,这个错误每次都会出现。下面是这个错误产生的输出。
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下却不行呢?我可以清楚地看到这个单选按钮是可见的。而且我手动也能点击它。那么为什么Selenium就不行呢?
4 个回答
0
你在使用的定位器值在IE浏览器中必须更改,因为IE的表现和Firefox(FF)以及Chrome是不同的。试着使用那些专门为IE设计的定位器,或者那些在IE中可以看到的定位器。
0
你可以试着关闭浏览器的原生事件。感谢Jim Evans对原生事件做了这么详细的解释。
profile = driver = webdriver.Ie()
profile.native_events_enabled = False
driver = webdriver.Ie(profile)
2
Selenium的Internet Explorer浏览器驱动并不是浏览器自带的,常常会在找到屏幕上的元素时出现问题。我猜这就是你遇到的问题。
1
首先,试着把你期待的条件改成 visibility_of_element_located
:
这个条件是用来检查一个元素是否在网页的DOM中存在并且是可见的。可见性意味着这个元素不仅被显示出来,而且它的高度和宽度都大于0。
注意,你不需要再次找到这个按钮,它会返回这个网页元素:
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'MyRadioButtonID')))
element.click()
另外,对于IE浏览器,设置一个更长的 隐式等待时间 可能会有好的效果:
driver.implicitly_wait(10) # seconds