简体   繁体   English

如何使用 Python Selenium 在没有 ID 或名称的页面上单击“接受 Cookie”

[英]How to Click “Accept Cookies” on a page without an ID or Name with Python Selenium

i am trying to figure out how to click a button called "Alles akzeptieren" to accept the cookies of the website.我想弄清楚如何单击一个名为“Alles akzeptieren”的按钮来接受网站的 cookies。 On other websites I used an ID or a name where it worked.在其他网站上,我使用了 ID 或名称。 But on this website I can't get access to the button and click it.但是在这个网站上,我无法访问该按钮并单击它。 The HTML looks like this: HTML 看起来像这样:

<button tabindex="0" title="Alle akzeptieren" aria-label="Alle akzeptieren" class="message-component message-button no-children button-responsive-primary" path="[0,1,1,0]" style="padding: 5px 0px; margin: 0px; border-width: 1px; border-color: rgb(50, 100, 255); border-radius: 4px; border-style: solid; font-size: 14px; font-weight: 500; color: rgb(255, 255, 255); font-family: verdana, geneva, sans-serif; width: calc(100% - 0px); background: rgb(50, 100, 255);">Alle akzeptieren</button>

The Xpath is: /html/body/div/div[2]/div[3]/div[2]/button Xpath 为:/html/body/div/div[2]/div[3]/div[2]/button

I tried the following code:我尝试了以下代码:

#accept cookies
time.sleep(5)
AcceptCookies = driver.find_element_by_link_text_name("Alles Akzeptieren")
AcceptCookies.click

Or或者

#accept cookies
time.sleep(5)
AcceptCookies = driver.find_element_by_class_name("message-component message-button no-children button-responsive-primary")
AcceptCookies.click

Does somebody know what the problem is?有人知道问题是什么吗? When I use this code I get the Error:当我使用此代码时,我收到错误:

NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".message-component message-button no-children button-responsive-primary"}
  (Session info: chrome=89.0.4389.90)

Or或者

AttributeError: 'WebDriver' object has no attribute 'find_element_by_link_text_name'

there is no href attribute in your button so find_element_by_link_text will not work you can use xpath您的按钮中没有 href 属性,因此 find_element_by_link_text 将不起作用您可以使用 xpath

Try with this css locator:试试这个 css 定位器:

AcceptCookies = driver.find_element_by_css_selector("button[title='Alle akzeptieren']")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM