Py学习  »  Python

Python接受cookies

JD. • 3 年前 • 1153 次点击  

我需要接受某个特定网站上的cookies,但我会不断收到 NoSuchElementException .这是进入网站的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time


chrome_options = Options()
driver = webdriver.Chrome(executable_path='./chromedriver', options=chrome_options)

page_url = 'https://www.boerse.de/historische-kurse/Erdgaspreis/XD0002745517'
driver.get(page_url)

time.sleep(10)

我尝试通过以下方式接受cookie按钮:

driver.find_element_by_class_name('message-component message-button no-children focusable button global-font sp_choice_type_11 last-focusable-el').click()
driver.find_element_by_xpath('//*[@id="notice"]').click()
driver.find_element_by_xpath('/html/body/div/div[2]/div[4]/div/button').click()

说到硒,我是一个初学者,只是想用它做一个简短的变通。谢谢你的帮助。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130933
 
1153 次点击  
文章 [ 1 ]  |  最新文章 3 年前
cruisepandey
Reply   •   1 楼
cruisepandey    3 年前

按钮 Zustimmen iframe 因此,首先你必须切换到相应的iframe,然后你可以与该按钮进行交互。

代码:

driver.maximize_window()
page_url = 'https://www.boerse.de/historische-kurse/Erdgaspreis/XD0002745517'
driver.get(page_url)
wait = WebDriverWait(driver, 30)
try:
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[starts-with(@id,'sp_message_iframe')]")))
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Zustimmen']"))).click()
    print('Clicked successfully')
except:
    print('Could not click')
    pass

进口:

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