使用Selenium和Python无法定位元素

huangapple go评论61阅读模式
英文:

Unable to locate element using Selenium and Python

问题

我正在尝试使用Selenium和Python在浏览网页时点击“接受Cookie”按钮。

使用Selenium和Python无法定位元素

我已经使用检查元素找到了按钮的类名,然后在Python中使用.click()来点击它。虽然它不是一个链接,但如果它正常工作,接受Cookie弹出窗口将消失,但它并没有消失。

使用Selenium和Python无法定位元素

相反,我收到了这个错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".SubmitButton CookiePopup-button"}

以下是我运行的代码:

cookie_button = driver.find_element("class name", "SubmitButton CookiePopup-button").click()

感谢您的回复!

英文:

I'm trying to click on an accept cookies button while navigating a webpage using Selenium and Python.

使用Selenium和Python无法定位元素

I've used inspect element to find the class name of the button, and then .click() in Python to click on it. It isn't a link, but if it were working the accept cookies pop-up would disappear, and it isn't.

使用Selenium和Python无法定位元素

Instead, I'm getting this error:

>selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".SubmitButton CookiePopup-button"}

Here is the code I am running:

cookie_button = driver.find_element("class name", "SubmitButton CookiePopup-button").click()

Thanks for your responses!

答案1

得分: 0

这里的问题在于类属性中有空格,为了解决这个问题,你可以使用```By.CSS_SELECTOR```代替```"class name"```进行搜索,并使用以下选择器:

button[class='SubmitButton CookiePopup-button']

尝试看看下面的代码是否有效:

cookie_button = driver.find_element(By.CSS_SELECTOR, "button[class='SubmitButton CookiePopup-button']").click()

英文:

The problem here is that there are spaces in the class attribute, to get around this you can instead search by using By.CSS_SELECTOR instead of "class name" and use this selector:

button[class='SubmitButton CookiePopup-button']

try and see if the below works:

cookie_button = driver.find_element(By.CSS_SELECTOR, "button[class='SubmitButton CookiePopup-button']").click()

答案2

得分: 0

cookie_button = driver.find_element(By.CSS_SELECTOR, "button.SubmitButton.CookiePopup-button").click()
英文:

You may try this, see if it works

cookie_button = driver.find_element(By.CSS_SELECTOR, "button.SubmitButton.CookiePopup-button").click()

huangapple
  • 本文由 发表于 2023年4月11日 12:56:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定