【问题标题】:SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(., 'Medium')' is not a valid XPath expression error using SeleniumSyntaxError:无法在“文档”上执行“评估”:字符串“//div [contains(., 'Medium')”不是使用 Selenium 的有效 XPath 表达式错误
【发布时间】:2020-06-09 03:35:05
【问题描述】:

我使用 selenium 尝试在 html 中获取 div 类的值,但我遇到了如下问题

invalid selector: Unable to locate an element with the xpath expression //div[contains(., 'Medium') because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(., 'Medium')' is not a valid XPath expression.

下面是我的 html 和读取 html 值的代码。

HTML:

代码试验:

IWebElement element = driver.FindElement(By.XPath("//div[contains(., 'Medium')"));
String text =   element.GetAttribute("text");
Console.WriteLine(text);

【问题讨论】:

    标签: c# selenium selenium-webdriver xpath xpath-1.0


    【解决方案1】:

    你的xpath表达式少关闭]

    IWebElement element = driver.FindElement(By.XPath("//div[contains(., 'Medium')]"));
    String text = element.Text;
    Console.WriteLine(text);
    

    要提取文本,请使用element.Text;element.GetAttribute("innerHTML");

    【讨论】:

    • 它工作,谢谢!!!但我仍然无法在控制台中获取文本值。
    • @JonathanK 试试element.GetAttribute("innerHTML");element.Text;
    • 其实我想得到“Medium不像互联网上的其他平台”的价值,谢谢你的帮助
    • 谢谢您..您知道如何为元素设置值吗?我想将“Medium is not like other platform on the internet”的值更改为其他措辞。
    • @JonathanK 不,它在 <div> 标签内,如果手动无法更改文本,同样使用 selenium。
    【解决方案2】:

    此错误消息...

    invalid selector: Unable to locate an element with the xpath expression //div[contains(., 'Medium') because of the following error:
    SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(., 'Medium')' is not a valid XPath expression.
    

    ...暗示您使用的 XPath 不是有效的 XPath 表达式。


    你离得很近。您在构建 时错过了结束 ]

    实际上,您的代码行将是:

    IWebElement element = driver.FindElement(By.XPath("//div[contains(., 'Medium')]"));
    

    参考文献

    您可以在以下位置找到一些相关的相关讨论:

    【讨论】:

      猜你喜欢
      • 2021-02-19
      • 2019-12-25
      • 2021-08-30
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 2018-10-28
      相关资源
      最近更新 更多