【问题标题】:how to get a div span class element如何获取 div span 类元素
【发布时间】:2020-08-02 00:33:19
【问题描述】:

我正在尝试在我的控制台中显示数字 1,显然它不起作用,请提供任何建议

这是我的代码,显然它不起作用。

String error1 = driver.findElement(By.xpath("//div[@class='label label-error']/span[@class='sidebar-label']")).getText();

控制台错误输出

  org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='label label-error']/span[@class='sidebar-label']"}

【问题讨论】:

  • 您是否反转了 div 和 span 元素的属性? String error1 = driver.findElement(By.xpath("//div[@class='sidebar-label']/span[@class='label label-error']")).getText();
  • 对不起,你的意思是这个 driver.findElement(By.xpath("//span[@class='sidebar-label']/div[@class='label label-error']") ).getText(); ?同样的错误
  • 试试这个,我认为你的 xpath 是错误的//div[@class="sidebar-label"]/span
  • 您为 div 和 span 使用了错误的 class 属性值。使用@E.Wiest 建议的 xpath

标签: java html selenium selenium-webdriver xpath


【解决方案1】:

您的 Xpath 错误,请使用以下之一:

  1. //div[@class="sidebar-label"]/span
  2. //div[@class="sidebar-label"]/span[@class="label label-error"]
  3. //*[@class="label label-error"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多