我需要接受 cookie,但按钮没有名称或 ID。这与此问题有关,但目前没有答案。
根据我在网上找到的其他选项,我尝试了以下方法,但徒劳无功:
driver.find_element_by_xpath('/html/body/div/button[1]').click()
driver.find_element_by_xpath("//a[. = 'Accept all cookies']").click()
driver.find_element_by_class_name('button.accept-settings-button')
driver.find_element_by_name('Accept all cookies').click()
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//a[contains(., "Accept all cookies")]')))
button.click()
driver.find_element_by_css_selector("button.accept-settings-button").click()
driver.find_element_by_css_selector("body > div > button.accept-settings-button").click()
driver.switch_to_frame(driver.find_element_by_css_selector('main'))
这是网站:(https://careers.portmandentalcare.com/Search.aspx
您需要等待 cookie 出现)和 js 元素:
<div class="main"><h1>Cookie preferences</h1><h2>Our use of cookies</h2><p>We use necessary cookies to make our site work. We also like to set optional analytics cookies to help us improve it. You can switch these off if you wish. Using this tool will set a cookie on your device to remember your preferences.</p><div>For more detailed information about the cookies we use, see our <a class="policy-link" href="#">Cookies policy</a>.</div><button type="button" class="accept-settings-button" aria-describedby="necessary-note">Accept all cookies</button><div class="note" id="necessary-note">By accepting recommended settings you are agreeing to the use of both our necessary and analytical cookies</div><h2>Necessary (always active)</h2><p>Necessary cookies enable core functionality such as security, network management, and accessibility. You may disable these by changing your browser settings, but this may affect how the website functions.</p><h2 id="analytics">Analytics</h2><p id="analytics-note">We use analytical cookies to help us to improve our website by collecting and reporting information on how you use it. For more information on how these cookies work please see our Cookies policy. The cookies collect information in an anonymous form.</p><div><div class="toggle-button-wrapper"><b>Off</b> <button type="button" class="toggle-analytics-button" role="switch" aria-labelledby="analytics" aria-describedby="analytics-note" aria-checked="false"></button> <b>On</b></div></div><button type="button" class="save-settings-button">Save my settings</button></div>
谢谢你的帮助。
PS 使用具有这些设置的最新 chromedriver
chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-logging')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--remote-debugging-port=9222") # to avoid DevToolsActivePort file doesn't exist
if os_system == 'linux':
driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=chrome_options)
elif os_system == 'mac':
driver = webdriver.Chrome(executable_path='/Users/user1/chromedriver', options=chrome_options)
<iframe src="https://careers.portmandentalcare.com/ico-compliance/index.html?v=637472026522948225" class="ico-compliance-plugin" title="Cookie preferences" tabindex="0"></iframe>
由于元素在 iframe 中,请等待 iframe 可用并切换到它,然后单击该元素。
wait = WebDriverWait(driver, 30)
driver.get("https://careers.portmandentalcare.com/Search.aspx")
wait.until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_class_name("ico-compliance-plugin")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.accept-settings-button"))).click()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句