可見截圖:
html
定位出現「Webdriver Element is not currently visible」的報錯。 根據其餘人的經驗,須要查找元素是否中是否有隱藏屬性。 發現上面代碼中存在display: block;
,故須要用js來模擬點擊行爲。java
解決方案:web
###1.可先查看該元素是否存在:框架
yes = driver.find_element_by_id("notice-confirm").is_displayed() print yes
返回true,說明能夠元素存在。dom
若是返回false,有幾種可能:.net
(1)定位不許確。換個定位方式
(2)這個元素須要hover或者懸停纔會顯示出來。code
#定位到要懸停的元素 above =driver.find_element_by_id("xx") #對定位到的元素執行懸停操做 ActionChains(driver).move_to_element(above).perform()
###2.使用js去點擊按鈕orm
js = "document.getElementsByTagName('button')[0].click()"; driver.execute_script(js)
詳細解釋以下:htm
js = "document.getElementsByTagName('button')[0].click()";
表示定位該元素,並點擊。
可在Firefox的控制檯中的對應頁面執行上面的js,查看是否認位成功和點擊效果。
有其餘的定位方式可見,可見連接HTML DOM Document 對象對象
driver.execute_script(js)
表示調用這個js片斷。
調用js方法使用**execute_script(script, *args)** 在當前窗口/框架 同步執行javaScript 腳本:JavaScript的執行。 *參數:適用任何JavaScript腳本。 使用: driver.execute_script(‘document.title’)
###3.經過javaScript修改display的值
js = 'document.querySelectorAll("select")[0].style.display="block";' driver.execute_script(js) sel = driver.find_element_by_tag_name('select') Select(sel).select_by_value('opel')
document.querySelectorAll("select")[0].style.display="block": document.querySelectorAll("select") 選擇全部的select。 [0] 指定這一組標籤裏的第幾個。 style.display="block"; 修改樣式的display="block" ,表示可見。
執行完這句js代碼後,就能夠正常操做下拉框了。
其餘人的解決方法:
關於元素定位的問題
Webdriver Element is not currently visible 解決方法
How to force Selenium WebDriver to click on element which is not currently visible?
有什麼更好的定位方法,歡迎大神賜教
PS: js的操做一個元素的前提也是先定位它。 能夠讓它變成: display:block 或 display:line。再去定位操做
'var div = document.getElementsByName("select2"); div.attributes.removeNamedItem("style");' driver.execute_script(js)