简体   繁体   English

为什么Python中的Selenium不会点击弹出的隐私按钮?

[英]Why won't Selenium in Python click the pop-up privacy button?

I am having some issues with Selenium not clicking the pop-up privacy button on https://www.transfermarkt.com/我在 Selenium 没有点击https://www.transfermarkt.com/上的弹出隐私按钮时遇到一些问题

Here is my code so far:到目前为止,这是我的代码:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()

It comes up saying:它出现说:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[2]/div[3]/div[2]/button"} NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"/html/body/div/div[2]/div[3]/div[2]/button "}

Anyone have any thoughts?有人有什么想法吗?

I believe the issue is that the element you are trying to click is inside an iframe .我认为问题在于您尝试单击的元素位于iframe内。 In order to click that element you'll have to first switch to that frame.为了单击该元素,您必须首先切换到该框架。 I noticed the iframe has title="SP Consent Message" so I'll use a CSS Selector to identify it based on that.我注意到 iframe 有title="SP Consent Message"所以我将使用 CSS 选择器来识别它。 Your code with the added line:带有添加行的代码:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[title="SP Consent Message"]'))
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()

Note that you may have to switch back to the default frame to continue your test, I'm not 100% sure as that iframe has gone away.请注意,您可能必须切换回默认框架才能继续测试,我不能 100% 确定 iframe 已经消失。

Also it seems like some folks don't get that popup when the hit the website, not sure why, may be something you want to look in to to determine how you want to test this.此外,似乎有些人在访问网站时没有得到那个弹出窗口,不知道为什么,可能是你想要查看以确定你想如何测试它的东西。

Like @Prophet says you should improve the xpath for the button (again it seems to have a unique title so I would use CSS Selector 'button[title="ACCEPT ALL"]' ), but this works for me to click it.就像@Prophet 说你应该改进按钮的 xpath (它似乎有一个独特的标题,所以我会使用 CSS Selector 'button[title="ACCEPT ALL"]' ),但这对我来说可以点击它。

There are 2 issues here:这里有2个问题:

  1. You need to add wait / delay before accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button') to let the page load您需要在accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')之前添加等待/延迟来让页面加载
  2. It's very, very bad practice to use absolute XPaths.使用绝对 XPath 是非常非常糟糕的做法。 You have to define an unique, short and clear related XPath expression.您必须定义一个唯一、简短且清晰的相关 XPath 表达式。

Additionally, I see no pop-up privacy button when I open that page.此外,当我打开该页面时,我看不到弹出隐私按钮。 So, maybe that element is indeed not there.所以,也许那个元素确实不存在。

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

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