python Selenium如何接受cookies

nbysray5  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(96)

我试图刮Facebook,但我被困在开始。我想用webdriver构建一个scraper。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import os 
import wget
import time

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" :2}
chrome_options.add_experimental_option("prefs",prefs)

#specify the path to chromedriver.exe (download and save on your computer)
driver = webdriver.Chrome('/Users/Andrea/chromedriver_mac64', chrome_options = 
chrome_options)

#open the webpage
driver.get("http://www.facebook.com")

这部分正在工作,事实上它在Facebook页面中打开了Chrome上的一个新标签页。

#target username
username = WebDriverWait(driver, 
10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']")))
password = WebDriverWait(driver, 
10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='pass']")))

#enter username and password
username.clear()
username.send_keys("username")
password.clear()
password.send_keys("password")

#target the login button and click it
button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 
"button[type='submit']"))).click()

表单的字段中填写了用户名和密码,但单击按钮不起作用。我收到的错误

ElementClickInterceptedException: Message: element click intercepted: Element <button value="1" class="_42ft _4jy0 _6lth _4jy6 _4jy1 selected _51sy" name="login" data-testid="royal_login_button" type="submit" id="u_0_5_mO">...</button> is not clickable at point (888, 303). Other element would receive the click: <div class="_9xo4">...</div >(Session info: chrome=112.0.5615.137)

问题似乎是,在Facebook页面出现的cookie框架,但我不知道如何自动接受cookie。
screenshot

6yt4nkrj

6yt4nkrj1#

您可以在网站打开后接受cookie,并在尝试访问用户名/密码字段之前

#wait for the pop up and then click accept
cookie_accept_all = WebDriverWait(driver, 
10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid='cookie-policy-manage-dialog-accept-button']")))
cookie_accept_all.click()

相关问题