【问题标题】:Python Selenium, find text in spanPython Selenium,在跨度中查找文本
【发布时间】: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


【解决方案1】:

试试这样的:

driver.find_element_by_xpath("//span[@ng-bind=\"'@' + user.username\"]")

【讨论】:

    猜你喜欢
    • 2021-10-10
    • 2019-03-23
    • 2018-09-23
    • 2020-04-29
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    相关资源
    最近更新 更多