【问题标题】:Deeply rooted selenium web element Java根深蒂固的 selenium 网页元素 Java
【发布时间】:2016-06-16 19:34:34
【问题描述】:

我正在尝试访问元素深处的标题。这是html代码的图像

我希望文本位于突出显示的部分。这是我认为可以执行此操作的代码...

       element = driver.findElement(By.xpath("//li[@id='result_" + i+ " span[//class[span[//data-attribute]]]"));
        String tmpTitle = element.getText();
        System.out.println(tmpTitle);

但我得到 NoSuchElementException.. 有人可以帮我弄清楚如何获取突出显示的文本吗?谢谢!

【问题讨论】:

  • 您能否准确地说出您真正想要停用的文本?这是文本:data-attributte="ScanDisk 23GB 4 SDHC memory .......(label may change)"?还是title="ScanDisk 23GB 4 SDHC memory .......(label may change)"
  • @kordirko 数据属性
  • 请添加页面的HTML文本并删除图片。图片太难阅读,不如文字有用。强烈建议不要使用代码图片。

标签: java selenium


【解决方案1】:

此代码将从 h2 元素返回页面上所有结果的 data-attribute 属性。

driver.get("https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=san+disk");
List<WebElement> results = driver.findElements(By.cssSelector("li[id^='result_'] h2"));
for (WebElement result : results)
{
    System.out.println(result.getAttribute("data-attribute"));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 2019-07-20
    • 2023-03-07
    • 1970-01-01
    • 2020-06-21
    相关资源
    最近更新 更多