我试图在元素内深入访问标题。这是html代码的图像
我希望文本位于突出显示的部分。这是我认为会做的代码......
element = driver.findElement(By.xpath("//li[@id='result_" + i+ " span[//class[span[//data-attribute]]]"));
String tmpTitle = element.getText();
System.out.println(tmpTitle);
但我得到NoSuchElementException ..有人可以帮我弄明白如何获得突出显示的文字吗?谢谢!
答案 0 :(得分: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"));
}