英文:
Unable to locate element using Selenium and Python
问题
我正在尝试使用Selenium和Python在浏览网页时点击“接受Cookie”按钮。
我已经使用检查元素找到了按钮的类名,然后在Python中使用.click()
来点击它。虽然它不是一个链接,但如果它正常工作,接受Cookie弹出窗口将消失,但它并没有消失。
相反,我收到了这个错误:
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.
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.
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论