Python Selenium WebDriverWait() 与 element_to_be_clickable() 不起作用

问题描述 投票:0回答:1

这是我的代码:

第 1 部分:初始化驱动程序

from selenium import webdriver 
from selenium.webdriver import Chrome 
from selenium.webdriver.chrome.service import Service 
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions() 
options.headless = True

options.page_load_strategy = 'none' 
chrome_path = ChromeDriverManager().install() 
chrome_service = Service(chrome_path) 

driver = Chrome(options=options, service=chrome_service) 
driver.implicitly_wait(5)
driver.maximize_window()

第 2 部分:

# provided that url and next_button_query is initialized and correct
# provided that there is a way to check for next page

driver.get(url)
delay = 20 # seconds
while there_is_a_next_page:
   next_button = WebDriverWait(self.driver, delay).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 
   next_button_query)))
   next_button.click()

它仍然抛出

element not clickable exception
。当我使用
time.sleep(1)
让它休眠 1 秒时,它起作用了,我始终能够单击该按钮进入下一页。

修订第 2 部分:

# provided that url and next_button_query is initialized and correct
# provided that there is a way to check for next page

driver.get(url)
while there_is_a_next_page:
   time.sleep(1)
   next_button.click()

但是,如果页面很多,这个解决方案可能不可靠并且可能会造成浪费。有什么办法可以等待元素可点击后立即点击吗?

提前致谢。

python selenium-webdriver webdriverwait element-to-be-clickable
1个回答
0
投票

显然这是一个已知问题:请参阅此处,如果您的异常表明存在另一个元素将接收点击。

© www.soinside.com 2019 - 2024. All rights reserved.