【问题标题】:How to handle pop up websites with python selenium如何使用 python selenium 处理弹出网站
【发布时间】:2021-11-07 02:40:01
【问题描述】:

我正在从多个网站获取数据,我遇到了一个弹出窗口(见下图)。我尝试了多种方法,例如检查警报或获取驾驶员的窗户,以便我可以切换到它并关闭它。我真的被困住了,似乎无法找到解决办法。我见过解决方案,但这些拨号带有警报。有人有想法吗?

【问题讨论】:

  • 网站网址是公开的吗?
  • 在同一个/标签窗口上看起来像一个覆盖 div。 (这不是警报)右键单击它并选择“检查”以查看单击关闭按钮所需的目标。

标签: python selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

在 Selenium 中你可以指定等待给定的元素,所以如果这是已知的弹出窗口,那么:

  1. 在此弹出窗口中选择元素
  2. 等待检测到该元素
  3. 导航到十字按钮并关闭弹出窗口

【讨论】:

    【解决方案2】:

    我尝试了几种方法。都成功了,因为我在driver.get('url')后面加了time.sleep(10)

    from selenium import webdriver
    import time
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    
    driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
    driver.maximize_window()
    driver.implicitly_wait(30)
    driver.get("https://satellitenewsnetwork.com/")
    time.sleep(10) # None of the below method works if this is removed.
    wait = WebDriverWait(driver,30)
    
    driver.switch_to.frame(driver.find_element_by_id("ml-webforms-popup-2207054"))
    # First:
    closepopup = driver.find_element_by_xpath("//div[@class='mailerlite-popup']/a")
    print(closepopup.get_attribute("class"))
    closepopup.click()
    
    # Second:
    # popup = wait.until(EC.presence_of_element_located((By.XPATH,"//div[@class='mailerlite-popup']"))) # Tried with visibility_of_element_located too.
    # print(popup.get_attribute("class"))
    # closepopup = driver.find_element_by_xpath("//div[@class='mailerlite-popup']/a")
    # print(closepopup.get_attribute("class"))
    # closepopup.click()
    
    # Third:
    # closepopup = driver.find_element_by_xpath("//div[@class='mailerlite-popup']/a")
    # print(closepopup.get_attribute("class"))
    # driver.execute_script("arguments[0].click();",closepopup)
    #
    # Fourth:
    # closepopup = driver.find_element_by_xpath("//div[@class='mailerlite-popup']/a")
    # print(closepopup.get_attribute("class"))
    # ActionChains(driver).move_to_element(closepopup).click().perform()
    
    time.sleep(5)
    
    driver.quit()
    

    【讨论】:

      猜你喜欢
      • 2011-11-13
      • 1970-01-01
      • 2018-03-28
      • 2018-06-07
      • 2015-07-09
      • 2020-06-09
      • 2020-11-16
      • 2013-02-03
      • 1970-01-01
      相关资源
      最近更新 更多