python - 如何使用面对异常(selenium + python)的文本()定位元素?
问题描述
HTML
<span class="menu-text">Download App</span>
我的代码来定位元素
driver.find_element_by_css_selector("//span[contains(text(),'App')]")
或者
driver.find_element_by_css_selector("//span[contains(.,'App')]")
当我运行它时,报告了一个异常:
selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression "//span[contains(text(),'App')]" is invalid: InvalidSelectorError: '//span[contains(text(),'App')]' is not a valid selector: "//span[contains(text(),'App')]"
解决方案
由于您使用 xpath 表达式,请driver.find_element_by_xpath
改用
driver.find_element_by_xpath("//span[contains(text(),'App')]")
希望这可以帮助
推荐阅读
- internet-explorer - 一个网站没有在 IE 中加载,但只发生在我们的局域网上
- python - Python中如何按类的属性排序?
- android - 从 strings.xml 获取字符串时出现 NullPointerException
- java - 如何根据用户输入计算一年中的剩余天数
- r - 如何将行名更改一个位置
- three.js - 如何用随机粒子填充加载的 STL 网格(不是简单的形状,如 CUBE ETC),并在 three.js 中使用此几何体绑定动画
- asp.net-mvc - 从 mvc 的下拉列表中删除重复项
- php - 如何在 Wordpress 搜索表单上根据文本字符串查找类别
- javascript - D3JS:是否可以将检索值的函数传递给“转换,翻译”属性?
- python - 在 Dask 中预分散数据对象有优势吗?