【问题标题】:handle popups in selenium python在 selenium python 中处理弹出窗口
【发布时间】:2021-01-27 18:05:13
【问题描述】:

我目前正在开展一个项目,我正在通过 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() 

请帮助我如何绕过此弹出窗口。谢谢

【问题讨论】:

    标签: python selenium google-chrome


    【解决方案1】:

    我认为,此链接可能对您有所帮助: 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")
    

    【讨论】:

    • 谢谢兄弟我试过了,它给出了消息,但弹出窗口仍然存在 selenium.common.exceptions.TimeoutException: 消息:等待 PA 创建确认弹出窗口出现超时。
    • @Romeo 您是否尝试设置更大的超时时间?不是 3,而是 30,例如
    • shift_to.alert 在这里不起作用,它是不同类型的框,它会阻止鼠标在框外单击。您只能单击复选框和弹出窗口中可用的按钮,不能单击其他任何地方
    • 是的,我也试过 10 秒,虽然弹出窗口会立即出现
    • @Romeo 正如我现在所读到的,99% 的情况下我们无法通过 selenium 移动到此弹出窗口...(
    猜你喜欢
    • 1970-01-01
    • 2021-01-11
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    相关资源
    最近更新 更多