【问题标题】:I'm trying to automate a process on a website. How do I close a pop up window with selenium, when it is not clear when the popup window will appear?我正在尝试使网站上的流程自动化。当不清楚何时会出现弹出窗口时,如何使用 selenium 关闭弹出窗口?
【发布时间】:2020-12-08 09:54:17
【问题描述】:

我正在尝试用 python 中的 selenium 关闭一个弹出窗口,这不允许我的代码进一步执行。但我无法做到这一点。有一个弹出窗口询问我是否要注册,但它总是在不一致的时间弹出。有没有办法检查弹出窗口是否处于活动状态?

到目前为止我的代码:

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

    PATH = "C:\Program Files (x86)\chromedriver.exe";
    driver = webdriver.Chrome(PATH);



    driver.get("https://www.investing.com/news/")
    time.sleep(3)
    accept_cookies = driver.find_element_by_xpath('//*[@id="onetrust-accept-btn-handler"]');
    accept_cookies.click();
    

【问题讨论】:

    标签: python selenium web popup window


    【解决方案1】:

    所以您的问题是您不希望显示注册弹出窗口。

    我只是在戳站点的脚本,我找到了一个非常好的解决方法:将用户代理设置为gene。如果用户代理与某些移动浏览器匹配,则显示弹出窗口的脚本将被禁用。

    import time
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    opts = Options()
    opts.add_argument("user-agent=gene")
    driver = webdriver.Chrome(options=opts)
    
    # Adds the cookie into current browser context
    driver.get("https://www.investing.com/news/")
    
    time.sleep(60) # wait for a minute to see if the popup happens
    driver.quit()
    

    运行这段代码,观察页面,它不会显示注册弹窗。


    替代解决方法: 使用已关闭注册弹出窗口的 Chrome 配置文件,也不会显示弹出窗口

    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
    

    如果您确实需要在没有这些方法的情况下关闭弹出窗口,请使用 try/except 定义一个检查函数。在执行任何操作之前,调用此函数检查是否有弹出窗口,然后关闭弹出窗口,将状态保存到某个变量(这样您就不再检查它了)。由于该函数有try/except,它不会引发异常并且您的代码会运行。

    【讨论】:

    • 第一种添加 cookie 的方法对我不起作用。它仍然显示消息。我也无法弄清楚注册弹出窗口是如何激活的,现在不是时候,但我想也许是鼠标移动?我想尝试的是强制弹出窗口发生,然后使用 xpath 简单地关闭它
    • 第一种方法对我有用,但我更新了另一种方法,将用户代理设置为gene。根据我的测试,它根本不会显示弹出窗口。试着给我一个反馈:)。另一种方法是为您的驱动程序添加 ublock 扩展,对其进行配置,使其阻止所有弹出窗口和 cookie 问题...它将帮助您减少广告,减少加载时间,更容易自动化您的脚本。跨度>
    • 现在可以使用了!我唯一没有开始工作的就是向那个 chrome 浏览器添加用户配置文件,但这对我的代码工作并不重要
    【解决方案2】:

    您可以使用 2 种我能立即想到的快速方法来做到这一点。

    1:

    • 你会经常使用这个
    from selenium.webdriver.common.by import By
    
    from selenium.webdriver.support.ui import WebDriverWait
    
    from selenium.webdriver.support import expected_conditions as EC
    

    然后,当您想要获取需要时间加载的元素并且想要对其执行操作时,您可以使用 WebDriverWait 来实现它:

    wait = WebDriverWait(driver, 10)
    try:
        accept_cookies = wait.until(EC.presence_of_element_located((By.XPATH, "'//* 
        [@id=\"onetrust-accept-btn-handler\"]'")))
    catch:
        # probably you have a bad locator and the element doesn't exist or it needs more than 10 sec to load.
    else:
        # logic here is processed after successfully obtaining the pop up
        accept_cookies.click()
    
    • 我建议使用 ActionChains
    from selenium.webdriver.common.action_chains import ActionChains
    

    然后在你获得弹出元素后,点击如下:

    actions = ActionChains(driver)
    actions.move_to_element(accept_cookies).click().perform()
    

    这相当于将鼠标指针移动到弹出关闭按钮的中间并单击它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多