是否可以使用 Selenium Java 从 href 标签中获取书籍作者姓名值?
Is it possible to get books author name value from href tag with Selenium Java?
<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");
}
}
根据评论更新
我正在尝试从 amazon.com 上的搜索图书页面获取图书作者姓名,例如,您可以使用搜索查询 "Java "amazon 访问此 link。com/s/... ,我试图从那里获取作者姓名。
嗯,从 URL 我可以提取信息。
@Test
public static void testMF(){
WebDriver driver;
System.setProperty("webdriver.gecko.driver","./src/drivers/geckodriver64bit.exe");
driver=new FirefoxDriver();
driver.get("https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=Java&rh=i%3Aaps%2Ck%3AJava");
List<WebElement> authorNames=driver.findElements(By.xpath("//a[contains(@href,'/Cay-S.-Horstmann/')]"));
for (WebElement author:authorNames){
System.out.println(author.getText());
}
}
这是带有作者姓名的控制台输出:
[RemoteTestNG] detected TestNG version 6.14.2
1542125453774 mozrunner::runner INFO Running command: "C:\Program Files\Mozilla Firefox\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\Users\Dhamo\AppData\Local\Temp\rust_mozprofile.ZTVooPdHTxOZ"
1542125455373 Marionette INFO Listening on port 63531
1542125455395 Marionette WARN TLS certificate errors will be ignored for this session
Nov 13, 2018 10:10:55 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Cay S. Horstmann
Cay S. Horstmann
更新问题的代码:
public static void testMF() {
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "./src/drivers/geckodriver64bit.exe");
driver = new FirefoxDriver();
driver.get(
"https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=Java&rh=i%3Aaps%2Ck%3AJava");
List<WebElement> searchResults = driver.findElements(By
.xpath("//div[@class='s-item-container'][1]/div/div/div[@class='a-fixed-left-grid-col a-col-right']"));
System.out.println(searchResults.size());
try {
if (searchResults.size() > 0) {
String title, author = null;
int i = 0;
for (WebElement el : searchResults) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", el);
i++;
title = el.findElement(By.cssSelector("h2.a-size-medium.s-inline.s-access-title.a-text-normal"))
.getText();
System.out.println("Title:" + i + "-" + title);
if(el.findElements(By.cssSelector("div.a-row.a-spacing-small > div:nth-child(2)")).size()>0){
author = el.findElement(By.cssSelector("div.a-row.a-spacing-small > div:nth-child(2)")).getText();
}
System.out.println("Author:" + i + "-" + (author.isEmpty()?"Author Not Found":author));
}
} else
System.out.println("Your search has no results");
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
根据您的问题更新以检索 书籍作者姓名 您需要诱导 WebDriverWait 以使所需元素可见,并且您可以使用以下任一解决方案:
cssSelector
:
List<WebElement> author_name_elements = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("ul#s-results-list-atf span.a-size-small.a-color-secondary>a.a-link-normal.a-text-normal")));
for (WebElement author_name:author_name_elements){
System.out.println(author_name.getText());
XPATH
:
List<WebElement> author_name_elements = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@id='s-results-list-atf']//span[contains(.,'by')]//following::span[1]/a[@class='a-link-normal a-text-normal']")));
for (WebElement author_name:author_name_elements){
System.out.println(author_name.getText());
<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");
}
}
根据评论更新
我正在尝试从 amazon.com 上的搜索图书页面获取图书作者姓名,例如,您可以使用搜索查询 "Java "amazon 访问此 link。com/s/... ,我试图从那里获取作者姓名。
嗯,从 URL 我可以提取信息。
@Test
public static void testMF(){
WebDriver driver;
System.setProperty("webdriver.gecko.driver","./src/drivers/geckodriver64bit.exe");
driver=new FirefoxDriver();
driver.get("https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=Java&rh=i%3Aaps%2Ck%3AJava");
List<WebElement> authorNames=driver.findElements(By.xpath("//a[contains(@href,'/Cay-S.-Horstmann/')]"));
for (WebElement author:authorNames){
System.out.println(author.getText());
}
}
这是带有作者姓名的控制台输出:
[RemoteTestNG] detected TestNG version 6.14.2
1542125453774 mozrunner::runner INFO Running command: "C:\Program Files\Mozilla Firefox\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\Users\Dhamo\AppData\Local\Temp\rust_mozprofile.ZTVooPdHTxOZ"
1542125455373 Marionette INFO Listening on port 63531
1542125455395 Marionette WARN TLS certificate errors will be ignored for this session
Nov 13, 2018 10:10:55 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Cay S. Horstmann
Cay S. Horstmann
更新问题的代码:
public static void testMF() {
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "./src/drivers/geckodriver64bit.exe");
driver = new FirefoxDriver();
driver.get(
"https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=Java&rh=i%3Aaps%2Ck%3AJava");
List<WebElement> searchResults = driver.findElements(By
.xpath("//div[@class='s-item-container'][1]/div/div/div[@class='a-fixed-left-grid-col a-col-right']"));
System.out.println(searchResults.size());
try {
if (searchResults.size() > 0) {
String title, author = null;
int i = 0;
for (WebElement el : searchResults) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", el);
i++;
title = el.findElement(By.cssSelector("h2.a-size-medium.s-inline.s-access-title.a-text-normal"))
.getText();
System.out.println("Title:" + i + "-" + title);
if(el.findElements(By.cssSelector("div.a-row.a-spacing-small > div:nth-child(2)")).size()>0){
author = el.findElement(By.cssSelector("div.a-row.a-spacing-small > div:nth-child(2)")).getText();
}
System.out.println("Author:" + i + "-" + (author.isEmpty()?"Author Not Found":author));
}
} else
System.out.println("Your search has no results");
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
根据您的问题更新以检索 书籍作者姓名 您需要诱导 WebDriverWait 以使所需元素可见,并且您可以使用以下任一解决方案:
cssSelector
:List<WebElement> author_name_elements = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("ul#s-results-list-atf span.a-size-small.a-color-secondary>a.a-link-normal.a-text-normal"))); for (WebElement author_name:author_name_elements){ System.out.println(author_name.getText());
XPATH
:List<WebElement> author_name_elements = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@id='s-results-list-atf']//span[contains(.,'by')]//following::span[1]/a[@class='a-link-normal a-text-normal']"))); for (WebElement author_name:author_name_elements){ System.out.println(author_name.getText());