简体   繁体   English

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

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

I'm trying to click a button using Selenium, but everytime I get the message that it can't find the element.我正在尝试使用 Selenium 单击一个按钮,但每次我收到它无法找到该元素的消息时。 This happens even when I put a time.sleep() in front of it.即使我在它前面放了一个 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()

The button is inside of a frame (not iframe), but I don't know how to switch to it.该按钮位于框架内(不是 iframe),但我不知道如何切换到它。 Here is the frame source code and page source code: https://www.codepile.net/pile/ZoG0REzQ .这里是框架源代码和页面源代码: https : //www.codepile.net/pile/ZoG0REzQ Can anyone help me out?谁能帮我吗?

If it's iframe :如果是 iframe :

the I would suggest you to switch to frame like this (with explicit waits):我建议你切换到这样的框架(显式等待):

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

Imports :进口:

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

once you have switched you can click like this :切换后,您可以像这样单击:

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

and click on close :然后点击关闭:

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

and once you are done, you should switch to default content like this :完成后,您应该切换到这样的默认内容:

driver.switch_to.default_content()

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

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