python selenium 接受 cookie iframe
python selenium accept cookie iframe
我是 python 和 selenium 的新手,我需要帮助。我无法在 iframe
中接受 cookie
有人可以帮帮我吗?
谢谢
from selenium.webdriver.firefox.options import Options
from seleniumwire import webdriver
options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(executable_path=r'C:\geckodriver.exe', firefox_options=options)
driver.get('https://autoscout24.de/')
time.sleep(5)
# go to iframe accept cookies
driver.switch_to.frame("gdpr-consent-notice")
driver.find_element_by_css_selector("button[class='mat-focus-indicator solo-button mat-button mat-button-base mat-raised-button cdk-focused cdk-mouse-focused'] div[class='action-wrapper']").cick()
# back to previous frame
driver.switch_to.parent_frame()
time.sleep(5)
driver, quit()
我看到 ID 是唯一的,那么您根本不需要 CSS Selector
。
你应该引导 Explicit waits 切换到 iframe
。
它们允许您的代码暂停程序执行或冻结线程,直到您传递给它的条件得到解决。以特定频率调用条件,直到等待超时结束。这意味着只要条件 returns 为假值,它就会继续尝试和等待。
代码:
driver = webdriver.Firefox(executable_path=r'C:\geckodriver.exe', firefox_options=options)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.get('https://autoscout24.de/')
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "gdpr-consent-notice")))
button = driver.find_element_by_id('save')
driver.execute_script("arguments[0].click();", button)
# back to previous frame
driver.switch_to.parent_frame()
time.sleep(5)
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我是 python 和 selenium 的新手,我需要帮助。我无法在 iframe
中接受 cookie有人可以帮帮我吗?
谢谢
from selenium.webdriver.firefox.options import Options
from seleniumwire import webdriver
options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(executable_path=r'C:\geckodriver.exe', firefox_options=options)
driver.get('https://autoscout24.de/')
time.sleep(5)
# go to iframe accept cookies
driver.switch_to.frame("gdpr-consent-notice")
driver.find_element_by_css_selector("button[class='mat-focus-indicator solo-button mat-button mat-button-base mat-raised-button cdk-focused cdk-mouse-focused'] div[class='action-wrapper']").cick()
# back to previous frame
driver.switch_to.parent_frame()
time.sleep(5)
driver, quit()
我看到 ID 是唯一的,那么您根本不需要 CSS Selector
。
你应该引导 Explicit waits 切换到 iframe
。
它们允许您的代码暂停程序执行或冻结线程,直到您传递给它的条件得到解决。以特定频率调用条件,直到等待超时结束。这意味着只要条件 returns 为假值,它就会继续尝试和等待。
代码:
driver = webdriver.Firefox(executable_path=r'C:\geckodriver.exe', firefox_options=options)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.get('https://autoscout24.de/')
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "gdpr-consent-notice")))
button = driver.find_element_by_id('save')
driver.execute_script("arguments[0].click();", button)
# back to previous frame
driver.switch_to.parent_frame()
time.sleep(5)
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC