【发布时间】:2018-11-13 14:41:55
【问题描述】:
<a class="a-link-normal a-text-normal"
href="/Cay-S.-Horstmann/e/B000AQ1QDY/ref=sr_ntt_srch_lnk_1?qid=1542117551&sr=8-1-spons">
Cay S. Horstmann
</a>
我的意思是可以从上面的标签中获取 Cay S. Horstmann 文本吗?
现在我正在尝试这样:
link.findElement(By.cssSelector("a.a-link-normal.a-text-normal")).getText());
但它只是输出一个空字符串...
public class AmazonSearchResultsPage {
public AmazonSearchResultsPage(WebDriver driver) {
PageFactory.initElements(driver, this);
this.driver = driver;
}
public WebDriver driver;
@FindBy(css = "#s-results-list-atf")
public WebElement searchResults;
public void getBooksInfo () {
List<WebElement> links = searchResults.findElements(By.tagName("li"));
if (links.size() > 0) {
String title, author, price, rating, isBestSeller;
for (int i = 0; i < links.size(); i++) {
title = links.get(i).findElement(By.cssSelector("h2.a-size-medium.s-inline.s-access-title.a-text-normal")).getText();
author = links.get(i).findElement(By.cssSelector("a.a-link-normal.a-text-normal")).getText());
}
}
else System.out.println("Your search has no results");
}
}
从 cmets 更新
我正在尝试从 amazon.com 上的搜索图书页面获取图书作者姓名,例如,您可以使用搜索查询“Java”amazon.com/s/...访问此链接,我尝试从那里获取作者姓名.
【问题讨论】:
-
尝试使用 getAttribute("value")
-
输出为空,不工作。
-
您是否尝试过使用其他定位器? 例如:xpath: //a[contains(@href,'/Cay-S.-Horstmann/')]' 还要检查是否存在框架。
-
不走运。更具体地说,我正在尝试从 amazon.com 上的搜索书籍页面获取书籍作者姓名,例如,您可以使用搜索查询“Java”amazon.com/s/… 访问此链接,我试图从那里获取作者姓名。你说的帧是什么意思?
-
好吧,我可以从网址中提取作者姓名。我没有发现定位器有问题。
标签: java selenium xpath css-selectors webdriverwait