【问题标题】:Handling “Accept All” (cookies) popup - Using Selenium in Python处理“全部接受”(cookies)弹出窗口 - 在 Python 中使用 Selenium
【发布时间】:2021-05-19 20:24:20
【问题描述】:

我正在尝试抓取一个足球统计数据网站,但为了访问该网站,我需要“接受所有”cookie。这只发生在我使用机器人时,而不是当我手动访问网站时。

import time
from selenium import webdriver
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(10)
driver.get('https://www.transfermarkt.com')
    
# click through annoying pop-up
popup = driver.find_element_by_xpath("//button[contains(@title,'ACCEPT ALL')]")
popup.click()

我认为(?)我已经正确编码了定位器部分。似乎在导航弹出窗口时遇到问题。基本结构如下:

<button tabindex="0" title="ACCEPT ALL" aria-label="ACCEPT ALL" class="message-component message-button no-children" path="[0,1,1,0]" style="padding: 10px 15px; margin: 5px 10px; border-width: 2px; border-color: rgb(92, 166, 255); border-radius: 4px; border-style: solid; font-size: 14px; font-weight: 400; color: rgb(255, 255, 255); font-family: verdana, geneva, sans-serif; width: 225px; background: rgb(0, 25, 63);">ACCEPT ALL </button>

谷歌搜索似乎表明我可能需要切换框架?任何帮助表示赞赏!下面的浏览器快照...

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping screen-scraping


    【解决方案1】:

    Cookie 窗口位于不同的框架内,使用以下方式导航到该框架:

    WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[@id="sp_message_iframe_382445"]')))
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(@title,'ACCEPT ALL')]"))).click()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 2013-02-03
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      相关资源
      最近更新 更多