网页抓取 - 接受 Cookies - SELENIUM - PYTHON - AIRBNB
Web scraping- Accept Cookies- SELENIUM - PYTHON - AIRBNB
我正在尝试接受 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()
在爱彼迎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.visibility_of_element_located((By.CSS_SELECTOR, "button.accept-cookies-button"))).click()
另外,我不明白你为什么要定义这么小的屏幕尺寸?
我们通常使用
options.add_argument('--window-size=1920,1080')
此外,它应该是--
,就像我在这里使用的那样
我可以在 airbnb 中看到这个 HTML :
<button data-testid="accept-btn" type="button" class="_1xiwgrva">OK</button>
您可以像这样点击接受 cookies 按钮:
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()
我正在尝试接受 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()
在爱彼迎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.visibility_of_element_located((By.CSS_SELECTOR, "button.accept-cookies-button"))).click()
另外,我不明白你为什么要定义这么小的屏幕尺寸?
我们通常使用
options.add_argument('--window-size=1920,1080')
此外,它应该是--
,就像我在这里使用的那样
我可以在 airbnb 中看到这个 HTML :
<button data-testid="accept-btn" type="button" class="_1xiwgrva">OK</button>
您可以像这样点击接受 cookies 按钮:
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()