【问题标题】:Web scraping- Accept Cookies- SELENIUM - PYTHON - AIRBNB网页抓取 - 接受 Cookies - SELENIUM - PYTHON - AIRBNB
【发布时间】:2021-10-15 08:02:36
【问题描述】:

我正在尝试接受 airbnb 主页中的 cookie。但我找不到得到它的“钥匙”。 在下面找到我的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep

options = Options()
# options.add_argument('--headless')
options.add_argument('window-size=400,800')


navegador = webdriver.Chrome(options=options)

navegador.get('https://www.airbnb.com/')


# I tried this 02 ways
WebDriverWait(navegador, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ""))).click()
WebDriverWait(navegador, 10).until(EC.element_to_be_clickable((By.XPATH, ""))).click()

在 Airbnb 的 HTML 下方查找

<button class="optanon-allow-all accept-cookies-button" title="OK" aria-label="OK" onclick="Optanon.TriggerGoogleAnalyticsEvent('OneTrust Cookie Consent', 'Banner Accept Cookies');" tabindex="1">OK</button>

【问题讨论】:

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


    【解决方案1】:

    我可以在 airbnb 中看到这个 HTML:

    <button data-testid="accept-btn" type="button" class="_1xiwgrva">OK</button>
    

    您可以像这样点击接受 cookie 按钮:

    WebDriverWait(navegador, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid='accept-btn']"))).click()
    

    但是我明白了,你已经分享了这个 html :-

    <button class="optanon-allow-all accept-cookies-button" title="OK" aria-label="OK" onclick="Optanon.TriggerGoogleAnalyticsEvent('OneTrust Cookie Consent', 'Banner Accept Cookies');" tabindex="1">OK</button>
    

    在这种情况下,您可以使用以下代码:

    WebDriverWait(navegador, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.optanon-allow-all.accept-cookies-button"))).click()
    

    【讨论】:

      【解决方案2】:

      试试这个:

      WebDriverWait(navegador, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.accept-cookies-button"))).click()
      

      另外,我不明白你为什么要定义这么小的屏幕尺寸?
      我们通常使用

      options.add_argument('--window-size=1920,1080')
      

      另外,这里应该是--,就像我在这里使用的那样

      【讨论】:

        猜你喜欢
        • 2019-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-25
        • 2019-04-27
        • 2013-11-13
        相关资源
        最近更新 更多