繁体   English   中英

无法使用 Selenium(Python)单击按钮

[英]Not able to click button with Selenium (Python)

我正在尝试使用 Selenium 单击一个按钮,但每次我收到它无法找到该元素的消息时。 即使我在它前面放了一个 time.sleep() 也会发生这种情况。

time.sleep(5)

                #Click on download
                downloadButton = driver.find_element_by_css_selector('#downloadButton')
                time.sleep(5)
                downloadButton.click()

                #Click on close
                closeButton = driver.find_element_by_xpath('//*[@id="btnZorgAnnuleren"]')
                closeButton.click()

该按钮位于框架内(不是 iframe),但我不知道如何切换到它。 这里是框架源代码和页面源代码: https : //www.codepile.net/pile/ZoG0REzQ 谁能帮我吗?

如果是 iframe :

我建议你切换到这样的框架(显式等待):

wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iframe ID")))

进口:

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

切换后,您可以像这样单击:

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#downloadButton"))).click()

然后点击关闭:

 wait.until(EC.element_to_be_clickable((By.ID, "btnZorgAnnuleren"))).click()

完成后,您应该切换到这样的默认内容:

driver.switch_to.default_content()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM