[英]handle popups in selenium python
我目前正在開展一個項目,我正在通過 url 自動化 whatsapp 消息。 像這樣https://wa.me/number_here
每當我正常執行此操作時,一切都很好,但是當我嘗試自動執行此操作時,會出現一個whatsapp彈出框並阻止所有內容,我的意思是所有內容,沒有右鍵單擊沒有開發人員選項,這就是為什么我無法獲得按鈕的x路徑的原因那個彈出窗口。 我試過(driver.shift_to,alert.close)但它說沒有這樣的警報。 我試圖通過 contains(text) 方法找到按鈕,但也沒有用。 這是我的代碼。
chrome_options = Options()
chrome_options.add_argument('--user-data-dir = user-data')
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_options.add_argument('--disable-notifications')
chrome_options.add_argument('--disable-popup-blocking')
chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chrome_options, executable_path= driver_path)
wait = WebDriverWait(driver, 60)
contact = f'https://wa.me/{number}'
driver.get(contact)
time.sleep(3)
alert = wait.until(EC.alert_is_present())
alert.accept()
請幫助我如何繞過此彈出窗口。 謝謝
我認為,此鏈接可能對您有所幫助: https://stackoverflow.com/a/19019311/12000849
我所做的是在我希望看到警報的點之前使用 WebDriverWait 設置條件延遲,然后切換到它,如下所示:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
browser = webdriver.Firefox()
browser.get("url")
browser.find_element_by_id("add_button").click()
try:
WebDriverWait(browser, 3).until(EC.alert_is_present(),
'Timed out waiting for PA creation ' +
'confirmation popup to appear.')
alert = browser.switch_to.alert
alert.accept()
print("alert accepted")
except TimeoutException:
print("no alert")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.