带有span:contains(' string')& quot;的selenium.common.exceptions.InvalidSelectorException [英] selenium.common.exceptions.InvalidSelectorException with "span:contains('string')"

查看:138
本文介绍了带有span:contains(' string')& quot;的selenium.common.exceptions.InvalidSelectorException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firefox中的Selenium python.我正在尝试通过CSS选择器查找元素

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)

我遇到错误了

 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')"

在Selenium IDE中,我可以通过此字段成功找到元素,但是在Python中,它不起作用

In selenium IDE I am successfully able to find element by this field but in Python it is not working

推荐答案

Selenium中使用css_selector通过文本定位元素(尽管它可以在开发人员工具控制台中使用).唯一的可能性是xpath

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)

@FlorentB的评论:

a comment by @FlorentB:

控制台也不支持css selector,但是JQuery支持它.控制台中的$('...')document.querySelector的简写,通常在页面具有时会被JQuery覆盖.

A css selector is not supported by the console either, but JQuery supports it. The $('...') from the console is a shorthand for document.querySelector which is generally overridden with JQuery when the page has it.

这篇关于带有span:contains(' string')& quot;的selenium.common.exceptions.InvalidSelectorException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆