你好,我使用的是一个自动网页抓取器,但有时屏幕上会弹出一个接受cookie弹出窗口,阻止表单点击按钮。它影响了整个脚本,我不知道我该怎么写,如果cookie_pop_up.close((或类似的东西。获取cookie关闭的xpath?
我正在尝试登录snapchat这里是我的代码:
hrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f"--proxy-server=http://{random.choice(live_proxies)}")
driver = webdriver.Chrome("chromedriver.exe", options=chrome_options)
driver.set_window_position(-10000,0)
driver.get("https://accounts.snapchat.com/accounts/login")
if "Log in to Snapchat" in driver.page_source:
proxy_is_valid = True
user_input = driver.find_element_by_id("username")
user_input.send_keys(user)
passsword_input = driver.find_element_by_id("password")
passsword_input.send_keys(password)
login_button = driver.find_element_by_xpath("/html/body/div[1]/div/div/div[3]/article/div[1]/div/form/div[4]/button")
login_button.click()
if check_for_captcha_connectivity(driver):
solve(driver)
错误如下:
File "C:UsersAdministratorDesktoppythonlibsite-packagesseleniumwebdriverremotewebelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:UsersAdministratorDesktoppythonlibsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:UsersAdministratorDesktoppythonlibsite-packagesseleniumwebdriverremoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button type="submit" class="btn btn-lg btn-primary">...</button> is not clickable at point (507, 384). Other element would receive the click: <div class="cookie-popup">...</div>
(Session info: chrome=77.0.3865.75)
以下是阻止其登录的情况:https://imgur.com/gallery/sZroFq9
提前感谢!
更新我试过这个
login_button = driver.find_element_by_xpath("/html/body/div[1]/div/div/div[3]/article/div[1]/div/form/div[4]/button")
login_button.click()
if ElementClickInterceptedException:
time.sleep(1)
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[3]/div/div/div[3]/div[4]").click()
continue
仍然失败了奇怪粘贴的凹痕^
您需要首先同意Cookie同意,然后继续。要接受cookie同意,您需要诱导WebDriverWait等待element_to_be_clickable()
,您可以使用以下定位器策略之一:
-
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.cookie-popup"))).click() driver.find_element_by_xpath("/html/body/div[1]/div/div/div[3]/article/div[1]/div/form/div[4]/button").click()
-
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='cookie-popup']"))).click() driver.find_element_by_xpath("/html/body/div[1]/div/div/div[3]/article/div[1]/div/form/div[4]/button").click()
-
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC