首页 > 解决方案 > 未找到 Python selenium webdriver 元素

问题描述

我正在开发一个网络自动化项目,但我卡在一个按钮上,我的 python 程序无法找到该过滤器按钮的元素

该按钮的 html 代码是

<a role="button" class="list_filter_toggle icon-filter btn btn-icon" tabindex="0" 
title="" id="task_filter_toggle_image" 
aria-controls="taskfilterdiv" 
aria-expanded="false" data-original-title="Show / hide filter"><span 
class="sr-only">Show / hide filter</span></a>

我的代码是

btn=browser.find_element_by_xpath("//*[contains(text(), 'Show / hide filter'")
btn.click()

错误信息

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an 
element with the xpath expression //*[contains(text(), 'Show / hide filter' because of the following 
error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(text(), 'Show / 
hide filter'' is not a valid XPath expression.
(Session info: chrome=89.0.4389.82)
Stacktrace:
Backtrace:

请帮帮我,我需要点击按钮但找不到按钮元素

标签: pythonselenium

解决方案


from selenium import webdriver
website = "www.stackoverflow.com"
driver = webdriver.Firefox()
driver.get(website)

e = driver.find_element_by_xpath("//*[text()='Show / hide filter']")

print(e)

推荐阅读