Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何点击;“接受饼干”;不带名称的按钮&;Python Selinium中的ID_Python_Selenium_Selenium Chromedriver - Fatal编程技术网

如何点击;“接受饼干”;不带名称的按钮&;Python Selinium中的ID

如何点击;“接受饼干”;不带名称的按钮&;Python Selinium中的ID,python,selenium,selenium-chromedriver,Python,Selenium,Selenium Chromedriver,我需要接受cookies,但按钮没有名称或ID。这与此相关,但目前没有答案 根据我在网上找到的其他选项,我尝试了以下方法,但没有成功: driver.find_element_by_xpath('/html/body/div/button[1]').click() driver.find_element_by_xpath("//a[. = 'Accept all cookies']").click() driver.find_

我需要接受cookies,但按钮没有名称或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
(您需要等待cookies出现)和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>

可以使用任何属性查找元素: 在上面的元素中,使用accept all按钮的类

css选择器是:

button.accept-settings-button
xpath是

//buttun[@class="accept-settings-button"]
所以


您可以使用这三种方法中的任何一种

谢谢您的评论,但它不起作用。首先,我将
buttun
更改为
button
这是我在其他选项中仍然遇到的错误或类似错误:
InvalidSelectorException:Message:invalid selector:无法找到带有xpath表达式的元素//button[@class=“accept settings button”由于以下错误:SyntaxError:未能对“文档”执行“评估”:字符串“//button[@class=“accept settings button””不是有效的XPath表达式。
您错过了一个结束括号,更新了答案。这很有帮助。非常感谢。如果有人需要,这里有一个ref链接:
<iframe src="https://careers.portmandentalcare.com/ico-compliance/index.html?v=637472026522948225" class="ico-compliance-plugin" title="Cookie preferences" tabindex="0"></iframe>
driver.find_element_by_xpath('//buttun[@class="accept-settings-button]"').click()
driver.find_element_by_css_selector('buttun.accept-settings-button').click()
driver.find_element_by_css_selector('buttun[class="accept-settings-button]"').click()
<iframe src="https://careers.portmandentalcare.com/ico-compliance/index.html?v=637472026522948225" class="ico-compliance-plugin" title="Cookie preferences" tabindex="0"></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()