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

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

问题描述

我正在使用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中,它不起作用

Selenium不支持使用 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 for document.querySelector which is generally overridden with JQuery when the page has it.

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

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