簡體   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