I need to accept the cookies but the button has no name or ID. This is related to this issue but no answer as of now.
I tried the following, based on other options I found online but in vain:
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'))
Here is the website: https://careers.portmandentalcare.com/Search.aspx
(you need to wait for the cookies to appear) and the js element:
<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>
Thanks for your help.
P.S. using latest chromedriver with these settings
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)
question from:
https://stackoverflow.com/questions/65896641/how-to-click-on-accept-cookie-button-without-name-id-in-python-selinium 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…