带有span:contains('string')'的selenium.common.exceptions.InvalidSelectorException; [英] selenium.common.exceptions.InvalidSelectorException with "span:contains('string')"
问题描述
我正在使用Firefox中的Selenium python。我正在尝试通过CSS选择器查找元素
element = span:contains('控制面板')
my_driver.find_element_by_css_selector(element)
我遇到错误了
引发exception_class(消息,屏幕,堆栈跟踪)
selenium.common.exceptions.InvalidSelectorException:消息:给定CSS选择器表达式 span:contains('控制面板' )无效:InvalidSelectorError:'span:contains('控制面板')'不是有效的选择器: span:contains('控制面板')
在Selenium IDE中,我可以通过该字段成功找到元素,但是在Python中,它不起作用
css_selector
通过文本定位元素(尽管它可以在开发人员工具控制台中使用)。唯一的可能性是 xpath
element = // span [contains (text(),控制面板)]
my_driver.find_element_by_xpath(element)
编辑:@FlorentB的评论:
A
css选择器
由控制台提供,但JQuery支持。控制台中的$('...')
是document.querySelector
的简写,通常用页面有jQuery时。
I am working on selenium python in firefox. I am trying to find element by css selector
element = "span:contains('Control panel')"
my_driver.find_element_by_css_selector(element)
I am getting below error
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression "span:contains('Control panel')" is invalid: InvalidSelectorError: 'span:contains('Control panel')' is not a valid selector: "span:contains('Control panel')"
In selenium IDE I am successfully able to find element by this field but in Python it is not working
Using css_selector
to locate element by text is not supported in Selenium (although it will work in the developer tools console). The only possibility is xpath
element = "//span[contains(text(), 'Control panel')]"
my_driver.find_element_by_xpath(element)
Edit: a comment by @FlorentB:
A
css selector
is not supported by the console either, but JQuery supports it. The$('...')
from the console is a shorthand fordocument.querySelector
which is generally overridden with JQuery when the page has it.
这篇关于带有span:contains('string')'的selenium.common.exceptions.InvalidSelectorException;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!