简体   繁体   English

单击接受 cookies 按钮使用 Selenium

[英]Clicking Accept cookies button using Selenium

I am trying to automate some download of data and have an issue with accepting the cookies message.我正在尝试自动下载一些数据,并且在接受 cookies 消息时遇到问题。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By
from random import randint
import time

current_link = "https://platform.smapone.com/Portal/Account/Login?ReturnUrl=%2FPortal%2F"
driver = webdriver.Chrome(PATH)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get(current_link)
#driver.switch_to.frame("cookieConsentIframe")
#driver.switch_to.frame(driver.find_element_by_name('cookieConsentIframe'))

try:
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='cookieConsentIframe']")))
    print(1)
    driver.find_element(By.XPATH,'//button[@id="cookies-accept-all"]').click()
    #driver.find_element(By.XPATH,'//button[text()="Accept"]').click()
except:
    pass
 
time.sleep(randint(5,8))   
driver.quit()

The code runs through (prints also the 1) but never clicks the button.代码运行(也打印 1)但从不单击按钮。 Any suggestions?有什么建议么? Tried so many things already.已经尝试了很多东西。

You need to also wait out the button:您还需要等待按钮:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//button[@id="cookies-accept-all"]'))).click()

EDIT Here is a full example (selenium/chromedriver setup is for python, but you need to observe only the imports, and part after defining the browser):编辑这是一个完整的例子(selenium/chromedriver 设置适用于 python,但您只需要观察导入,并在定义浏览器后观察部分):

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC

import time as t

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)



url = 'https://platform.smapone.com/Portal/Account/Login?ReturnUrl=%2FPortal%2F'
browser.get(url)

WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='cookieConsentIframe']")))
    
## sortout cookie button
try:
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//button[@id="cookies-accept-all"]'))).click()
    print("accepted cookies")
except Exception as e:
    print('no cookie button')

The element Accept is within an <iframe> so you have to:元素Accept位于<iframe>中,因此您必须:

  • Induce WebDriverWait for the desired frame to be available and switch to it .诱导WebDriverWait使所需的帧可用并切换到它

  • Induce WebDriverWait for the desired element to be clickable .诱导WebDriverWait使所需元素成为可点击的。

  • You can use either of the following locator strategies :您可以使用以下任一定位器策略

    • Using CSS_SELECTOR :使用CSS_SELECTOR

       driver.execute("get", {'url': 'https://platform.smapone.com/Portal/Account/Login?ReturnUrl=%2FPortal%2F'}) WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.cookieConsent#cookieConsentIframe"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#cookies-accept-all span.btn-accept"))).click()
    • Using XPATH :使用XPATH

       driver.execute("get", {'url': 'https://platform.smapone.com/Portal/Account/Login?ReturnUrl=%2FPortal%2F'}) WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='cookieConsent' and @id='cookieConsentIframe']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='cookies-accept-all']//span[@class='btn-accept']"))).click()
  • Note : You have to add the following imports:注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
  • Browser Snapshot:浏览器快照:

斯马彭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM