【问题标题】:How do I close pop-up windows with Selenium in Python when I don't know when they will pop up?当我不知道它们何时会弹出时,如何在 Python 中使用 Selenium 关闭弹出窗口?
【发布时间】:2022-01-12 08:40:08
【问题描述】:

我正在尝试从该网站抓取历史天气数据: https://www.worldweatheronline.com/taoyuan-weather-history/tai-wan/tw.aspx

使用此代码:

    driver.find_element_by_css_selector("input[type='date']").send_keys(str(for_weather.mm[i])+str(for_weather.dd[i])+for_weather.year[i].astype(str))

wait=WebDriverWait(driver,10)
wait.until(EC.element_to_be_clickable((By.ID,'ctl00_MainContentHolder_butShowPastWeather'))).click()
temp=driver.find_element_by_xpath('//div[@class="days-collapse-temp"]').get_attribute('innerHTML')

其中一些页面会出现一个弹出窗口。

我看到了显示如何选择和关闭弹出窗口的帮助,但就我而言,我们不知道它们何时会出现。在某些页面上会出现,有些则不会。当它们出现时,它们会阻止我获取我想要的数据,并停止循环。以下是错误信息(具有弹窗的特点):

ElementClickInterceptedException: element click intercepted: Element <input type="submit" name="ctl00$MainContentHolder$butShowPastWeather" value="Get Weather" id="ctl00_MainContentHolder_butShowPastWeather" class="btn btn-success ml-2"> is not clickable at point (956, 559). Other element would receive the click: <div class="introjs-overlay" style="inset: 0px; position: fixed; cursor: pointer;"></div>
  (Session info: chrome=97.0.4692.71)

非常感谢!

【问题讨论】:

    标签: python-3.x selenium popup


    【解决方案1】:

    这是一个使用 SeleniumBase 的 Python Selenium 解决方案:

    首先pip install seleniumbase,然后将下面的示例复制到Python 文件中,例如weather_test.py。然后用pytest运行它:

    pytest weather_test.py --block-ads

    from seleniumbase import BaseCase
    
    class MyTestClass(BaseCase):
        def test_base(self):
            self.open("https://www.worldweatheronline.com/taoyuan-weather-history/tai-wan/tw.aspx")
            self.js_click("#ctl00_MainContentHolder_butShowPastWeather")
            for i in range(1, 32):
                date_string = "2021-12-%s" % i
                self.set_attribute("#datePicker input", "value", date_string)
                self.js_click('input[value="Get Weather"]')
                temp = self.get_attribute("div.days-collapse-temp", "innerHTML")
                temp = temp.split("</span>")[-1]
                print("%s : %s" % (date_string, temp))
    

    这将为您提供该城市 12 月所有日子的天气:

    2021-12-1 : 11°/13°c
    2021-12-2 : 11°/13°c
    2021-12-3 : 11°/13°c
    2021-12-4 : 11°/13°c
    2021-12-5 : 11°/13°c
    2021-12-6 : 11°/13°c
    2021-12-7 : 11°/13°c
    2021-12-8 : 11°/13°c
    2021-12-9 : 11°/13°c
    2021-12-10 : 17°/21°c
    2021-12-11 : 17°/25°c
    2021-12-12 : 17°/20°c
    2021-12-13 : 14°/15°c
    2021-12-14 : 15°/23°c
    2021-12-15 : 15°/28°c
    2021-12-16 : 17°/27°c
    2021-12-17 : 12°/18°c
    2021-12-18 : 11°/13°c
    2021-12-19 : 12°/18°c
    2021-12-20 : 15°/18°c
    2021-12-21 : 16°/18°c
    2021-12-22 : 17°/18°c
    2021-12-23 : 16°/19°c
    2021-12-24 : 16°/21°c
    2021-12-25 : 13°/14°c
    2021-12-26 : 9°/12°c
    2021-12-27 : 9°/11°c
    2021-12-28 : 10°/18°c
    2021-12-29 : 14°/17°c
    2021-12-30 : 12°/13°c
    2021-12-31 : 11°/14°c
    

    主要区别在于它使用js_click 来点击东西而不是常规点击,如果有弹出窗口,它会给你一个ElementClickInterceptedException

    【讨论】:

      【解决方案2】:
      try:
         wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'div.introjs-tooltiptext'))).click()
      except:
         pass
      

      您可以尝试处理弹出窗口或使用Javascript直接点击。

      element=wait.until(EC.presence_of_element_located((By.ID,'ctl00_MainContentHolder_butShowPastWeather')))
      driver.execute_script("arguments[0].click();", element)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-13
        • 1970-01-01
        • 1970-01-01
        • 2021-03-11
        • 1970-01-01
        • 1970-01-01
        • 2021-06-21
        • 2020-06-22
        相关资源
        最近更新 更多