Python selenium 没有点击 cookie 接受按钮

Python selenium doesn't click on on the cookie accept button

我想访问一个网站,但我无法让弹出的 cookie 消失。该网站本身打开,但我似乎无法抓住 cookie 的接受按钮。到目前为止,这是我的代码:

#set path, open firefox
path = r'C:\Users\Anwender\Downloads\geckodriver-v0.30.0-win64\geckodriver.exe'
driver = webdriver.Firefox(executable_path=path)

#open bym forum
driver.get('https://www.bym.de/forum/')

#deal with the effing cookie message
wait = WebDriverWait(driver,15)
wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[4]/div[2]/div/div/div/div/div/div/div[2]/div[2]/button[2]/span'))).click()

#log in
username = driver.find_element_by_id("navbar_username")
password = driver.find_element_by_id("navbar_password")
username.send_keys("my_username")
password.send_keys("my_password")
driver.find_element_by_xpath("/html/body/div[1]/div[2]/main/div[3]/div[1]/ul/li[2]/form/fieldset/div/div/input[4]").click()

我包含的登录代码提到我使用完全相同的语法登录到不同的网站,所以我不确定如何通过这个。 html Cookie 按钮“接受”(此处为:“Zustimmung”)的代码是:

<button title="Zustimmen" class="message-component message-button no-children focusable button global-font-headline sp_choice_type_11 last-focusable-el" style="padding: 14px 40px 12px; margin: 12px 0px; border-width: 1px; border-color: rgb(102, 102, 102); border-radius: 20px; border-style: solid; font-size: 12px; font-weight: 700; color: rgb(17, 17, 17); font-family: georgia, serif; width: calc(100% - 80px); background: rgb(255, 255, 255) none repeat scroll 0% 0%;">Zustimmen</button>

我也尝试通过 css 路径或名称来获取按钮,但我不知道为什么它不起作用。 :/

接受按钮位于 iframe 中,您需要先切换 iframe 才能访问该元素。

使用 webdriverwait() 并等待框架可用并切换。

//切换到iframe

WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']")))

//点击接受按钮

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[title='Zustimmen']"))).click()

您需要导入以下库。

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

元素 ZUSTIMMEN 中,因此您必须:

  • 诱导 WebDriverWait 所需的 帧可用并切换到它

  • 诱导 所需的 元素可点击

  • 您可以使用以下任一方式:

    • 使用CSS_SELECTOR:

      driver.get("https://www.bym.de/forum/")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Zustimmen']"))).click()
      
    • 使用XPATH:

      driver.get("https://www.bym.de/forum/")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='SP Consent Message']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@title='Zustimmen']"))).click()
      
  • 注意:您必须添加以下导入:

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

参考

您可以在以下位置找到一些相关讨论:

  • Switch to an iframe through Selenium and python