【发布时间】:2017-09-21 15:24:16
【问题描述】:
我正在尝试在页面上查找文本。使用 Selenium 的 Python 代码:
driver.find_element_by_xpath("//span[@ng-bind=''@' + user.username']")
我需要的文字“@bassale”在页面上:
执行代码时出现错误:
selenium.common.exceptions.InvalidSelectorException: Message: {"errorMessage":"Unable to locate an element with the xpath expression //span[@ng-bind=''@' + user.username'] 因为以下错误:\n错误:INVALID_EXPRESSION_ERR:DOM XPath 异常 51
帮我在页面上找到这段文字。
【问题讨论】:
-
您的 xpath 无效,因此您遇到了问题
-
@thebadguy 好的,但我将如何正确编写查询?
-
try "//span[@ng-bind=\"'@' + user.username\"]" 只是转义引号,参考stackoverflow.com/questions/32759318/…
-
使用 driver.find_element_by_xpath("//span[@ng-bind=\"'@' + user.username\"]").text 获取文本
-
by.xpath('//span[@ng-bind="{0}"]'.format("'@' + user.username"));或 by.xpath('//span[@ng-bind="\'@\' + user.username"]');
标签: python-3.x selenium