我使用Python 3.4和Selenium Webdriver python bindings。我在Windows機器上運行。 當我使用Selenium Chrome和Firefox webdrivers時,以下腳本可用於測試我的網站。但是,當我切換到IE Webdriver時,它失敗。這是我的腳本:爲什麼Selenium可以使用Chrome瀏覽器和Firefox點擊單選按鈕,但IE 9無法使用?
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:使用硒在座標上執行更粗略的點擊,而不是點擊按鈕。 – 2014-10-28 21:01:34
不幸的是,我無法更改我的IE版本。我需要在這個特定版本的IE上做實驗。 – 2014-12-10 18:51:42
你可以嘗試點擊使用JavascriptExecutor的元素。 – 2014-12-15 07:49:40