如何在 Python Selinium 中单击没有名称和 ID 的“接受 Cookie”按钮

吉布

我需要接受 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&nbsp;<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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何单击没有名称,ID或值的按钮?

如何使用 Selenium 和 Python 自动点击“接受 cookie”按钮

如何使用selinium python在列表中插入具有类似文本的所有id

如何点击与Java硒按钮没有名称或ID

如何使用Selinium Python在TD网络表中获取文本

Excel VBA 单击使用 JSON 的没有名称或 ID 的网站按钮

VBScript / WshShell-单击没有名称,ID或类的按钮

使用Python填写没有名称和ID的Web表单

python selenium通过div中的标记查找元素(没有名称,类,ID或文本)

当表单仅具有属性(即没有名称或ID)时,如何在scrapysharp中查找表单

如何在机器人框架中选择没有名称或ID的框架?

按钮未点击 selinium

VBA单击没有名称的HTML按钮?

如何告诉谷歌我接受cookie python

提交按钮没有名称时如何处理表单提交按钮

如何隐藏没有名称属性的odoo操作类型按钮?

如何在Laravel中的邮件中添加接受/拒绝按钮

Python Selenium 接受 cookie

Selenium 接受 cookie python

如何使用python3 -i加载脚本,但没有名称==主运行?

如何在ionic 2 angular 2中的单击事件中获取按钮的id和名称值

如何在python中接受以#开头的字符串?

如何在Python类构造函数中接受参数?

我如何在python中接受用户的嵌套字典

如何接受JavaScript的ID?

如何在Python脚本中接受管道输入,然后接受用户提示的输入?

如何选择和单击没有id属性的按钮

下拉列表中没有ID,也没有名称

如何在spring-mvc中接受具有相同ID的多个REST查询参数?