2017-02-13 63 views
1

我想取消選中使用Python中的硒的複選框。但是,我得到了以下錯誤消息:Python硒選擇隱形複選框

selenium.common.exceptions.ElementNotVisibleException:
消息:元素當前不可見,所以可能不會

我想知道如何進行交互應我使它可見?

這個複選框的一個有趣的部分是它包含了一些JavaScript,我不確定是否這是造成麻煩的地方。我嘗試了以下方法,但得到了相同的錯誤。

driver.find_element_by_id("1986 Thru 1990").click() 

driver.find_element_by_xpath('//*[@id="1986 Thru 1990"]').click() 

enter image description here

回答

1

嘗試添加一些時間才能等到元素變得可見:

from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 

wait = WebDriverWait(driver, 10) 
element = wait.until(EC.visibility_of_element_located((By.ID,'1986 Thru 1990'))) 
element.click() 

讓我知道,如果問題仍然存在

+0

謝謝爲你的建議離子。但它看起來像我有一個超時異常'selenium.common.exceptions.TimeoutException:消息:'。我應該增加超時限制嗎? –

+0

不可以。告訴我這個複選框最初是否可見,或者您需要向下滾動才能看到它或將鼠標指針懸停在其上以使其可見? – Andersson

+0

@ tao.hong如果你有這個超時異常,那麼這可能意味着你沒有用你的'xpath'來獲取正確的元素。你可能會得到一些不可見的元素。提供一些html可能會有幫助。 – mrfreester