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>
What I mean is it possible to get Cay S. Horstmann text from above tag?
Now I'm trying to get it like this:
link.findElement(By.cssSelector("a.a-link-normal.a-text-normal")).getText());
But it just outputs an empty string ...
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");
}
}
Update from comments
I'm trying to get books author name from searched books page on amazon.com, for example you can visit this link with search query "Java "amazon.com/s/… , I tried to get author name from there.
java selenium xpath css-selectors webdriverwait
add a comment |
<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>
What I mean is it possible to get Cay S. Horstmann text from above tag?
Now I'm trying to get it like this:
link.findElement(By.cssSelector("a.a-link-normal.a-text-normal")).getText());
But it just outputs an empty string ...
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");
}
}
Update from comments
I'm trying to get books author name from searched books page on amazon.com, for example you can visit this link with search query "Java "amazon.com/s/… , I tried to get author name from there.
java selenium xpath css-selectors webdriverwait
1
Try using getAttribute("value")
– Dhamo
Nov 13 at 15:44
The output is null, not working.
– Wonderio619
Nov 13 at 15:50
1
Have you tried with other locators? Eg: xpath: //a[contains(@href,'/Cay-S.-Horstmann/')]' also check there are no frames exist.
– Dhamo
Nov 13 at 15:53
No luck. To be more specific, I'm trying to get books author name from searched books page on amazon.com, for example you can visit this link with search querry "Java "amazon.com/s/… , I tried to get author name from there. And what you mean by frames ?
– Wonderio619
Nov 13 at 16:01
1
Well, I am able to extract the author names from the url. I don't see a problem with the locator.
– Dhamo
Nov 13 at 16:17
add a comment |
<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>
What I mean is it possible to get Cay S. Horstmann text from above tag?
Now I'm trying to get it like this:
link.findElement(By.cssSelector("a.a-link-normal.a-text-normal")).getText());
But it just outputs an empty string ...
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");
}
}
Update from comments
I'm trying to get books author name from searched books page on amazon.com, for example you can visit this link with search query "Java "amazon.com/s/… , I tried to get author name from there.
java selenium xpath css-selectors webdriverwait
<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>
What I mean is it possible to get Cay S. Horstmann text from above tag?
Now I'm trying to get it like this:
link.findElement(By.cssSelector("a.a-link-normal.a-text-normal")).getText());
But it just outputs an empty string ...
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");
}
}
Update from comments
I'm trying to get books author name from searched books page on amazon.com, for example you can visit this link with search query "Java "amazon.com/s/… , I tried to get author name from there.
java selenium xpath css-selectors webdriverwait
java selenium xpath css-selectors webdriverwait
edited Nov 14 at 20:45
marc_s
570k12811021250
570k12811021250
asked Nov 13 at 14:41
Wonderio619
146
146
1
Try using getAttribute("value")
– Dhamo
Nov 13 at 15:44
The output is null, not working.
– Wonderio619
Nov 13 at 15:50
1
Have you tried with other locators? Eg: xpath: //a[contains(@href,'/Cay-S.-Horstmann/')]' also check there are no frames exist.
– Dhamo
Nov 13 at 15:53
No luck. To be more specific, I'm trying to get books author name from searched books page on amazon.com, for example you can visit this link with search querry "Java "amazon.com/s/… , I tried to get author name from there. And what you mean by frames ?
– Wonderio619
Nov 13 at 16:01
1
Well, I am able to extract the author names from the url. I don't see a problem with the locator.
– Dhamo
Nov 13 at 16:17
add a comment |
1
Try using getAttribute("value")
– Dhamo
Nov 13 at 15:44
The output is null, not working.
– Wonderio619
Nov 13 at 15:50
1
Have you tried with other locators? Eg: xpath: //a[contains(@href,'/Cay-S.-Horstmann/')]' also check there are no frames exist.
– Dhamo
Nov 13 at 15:53
No luck. To be more specific, I'm trying to get books author name from searched books page on amazon.com, for example you can visit this link with search querry "Java "amazon.com/s/… , I tried to get author name from there. And what you mean by frames ?
– Wonderio619
Nov 13 at 16:01
1
Well, I am able to extract the author names from the url. I don't see a problem with the locator.
– Dhamo
Nov 13 at 16:17
1
1
Try using getAttribute("value")
– Dhamo
Nov 13 at 15:44
Try using getAttribute("value")
– Dhamo
Nov 13 at 15:44
The output is null, not working.
– Wonderio619
Nov 13 at 15:50
The output is null, not working.
– Wonderio619
Nov 13 at 15:50
1
1
Have you tried with other locators? Eg: xpath: //a[contains(@href,'/Cay-S.-Horstmann/')]' also check there are no frames exist.
– Dhamo
Nov 13 at 15:53
Have you tried with other locators? Eg: xpath: //a[contains(@href,'/Cay-S.-Horstmann/')]' also check there are no frames exist.
– Dhamo
Nov 13 at 15:53
No luck. To be more specific, I'm trying to get books author name from searched books page on amazon.com, for example you can visit this link with search querry "Java "amazon.com/s/… , I tried to get author name from there. And what you mean by frames ?
– Wonderio619
Nov 13 at 16:01
No luck. To be more specific, I'm trying to get books author name from searched books page on amazon.com, for example you can visit this link with search querry "Java "amazon.com/s/… , I tried to get author name from there. And what you mean by frames ?
– Wonderio619
Nov 13 at 16:01
1
1
Well, I am able to extract the author names from the url. I don't see a problem with the locator.
– Dhamo
Nov 13 at 16:17
Well, I am able to extract the author names from the url. I don't see a problem with the locator.
– Dhamo
Nov 13 at 16:17
add a comment |
2 Answers
2
active
oldest
votes
As per your question update to retrieve the books author name you need to induce WebDriverWait for the desired elements to be visible and you can use either of the following solutions:
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());
1
DebanjanB, it really works ! I knew there must be some way to handle this, thank you )) I will accept your answer, but may be you can help me with Best Seller check also. I updated main post with the way I handled isBestSeller check, it works, but took too much time for check like this, may be a better way to handle this check exists ...
– Wonderio619
Nov 14 at 10:32
1
@Wonderio619 Glad to be able to help you !!! Can you please raise a new question with your new requirement? As there are multiple Answers available so changing the question will make the other answers invalid and may not be useful to future readers. For the time being I am reverting back your question to the previous state. StackOverflow contributers will be happy to help you out with your new question.
– DebanjanB
Nov 14 at 10:36
1
DebanjanB, I created a new thread here regarding remaining problems, please join if you want stackoverflow.com/questions/53300695/…
– Wonderio619
Nov 14 at 12:53
add a comment |
Well, from the URL I am able to extract the info.
@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());
}
}
and here is the console output with the author name:
[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
Code for updated question:
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());
}
}
Dhamo, I got your point, but this is not exactly what I'm trying to do. I need to extract all authors names from search page. Not find author name by checking href. And you need to access href section and manually type author name for each search result for your code to work, this is not the way I want it to work. I updated main post with my code, I think you will understand what I'm trying to do.
– Wonderio619
Nov 13 at 16:30
1
Well, in that case, to extract all author names in the page, you need to change [to xpath: //span[@class='a-size-small a-color-secondary' and contains(.,'by')]/../span[2]] the locator as the one you have is not only locating the author names but locating many elements in the page.
– Dhamo
Nov 13 at 18:15
Dhamo, thank you, I will test your code tomorrow on internship, because my internet connection is very bad now. I see you used xpath for locating authors, is it somehow generated, or you write it using your own skills ? It is strange for me that I was not able to access simple linkText value from href by using cssselector, may be I should start learning xpath ...
– Wonderio619
Nov 13 at 19:55
1
I wrote it. Learning xpath is must. All the best!
– Dhamo
Nov 13 at 20:01
1
@Wonderio619 - Please check the new code now . It is working.
– Dhamo
Nov 14 at 18:01
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283467%2fis-it-possible-to-get-books-author-name-value-from-href-tag-with-selenium-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As per your question update to retrieve the books author name you need to induce WebDriverWait for the desired elements to be visible and you can use either of the following solutions:
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());
1
DebanjanB, it really works ! I knew there must be some way to handle this, thank you )) I will accept your answer, but may be you can help me with Best Seller check also. I updated main post with the way I handled isBestSeller check, it works, but took too much time for check like this, may be a better way to handle this check exists ...
– Wonderio619
Nov 14 at 10:32
1
@Wonderio619 Glad to be able to help you !!! Can you please raise a new question with your new requirement? As there are multiple Answers available so changing the question will make the other answers invalid and may not be useful to future readers. For the time being I am reverting back your question to the previous state. StackOverflow contributers will be happy to help you out with your new question.
– DebanjanB
Nov 14 at 10:36
1
DebanjanB, I created a new thread here regarding remaining problems, please join if you want stackoverflow.com/questions/53300695/…
– Wonderio619
Nov 14 at 12:53
add a comment |
As per your question update to retrieve the books author name you need to induce WebDriverWait for the desired elements to be visible and you can use either of the following solutions:
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());
1
DebanjanB, it really works ! I knew there must be some way to handle this, thank you )) I will accept your answer, but may be you can help me with Best Seller check also. I updated main post with the way I handled isBestSeller check, it works, but took too much time for check like this, may be a better way to handle this check exists ...
– Wonderio619
Nov 14 at 10:32
1
@Wonderio619 Glad to be able to help you !!! Can you please raise a new question with your new requirement? As there are multiple Answers available so changing the question will make the other answers invalid and may not be useful to future readers. For the time being I am reverting back your question to the previous state. StackOverflow contributers will be happy to help you out with your new question.
– DebanjanB
Nov 14 at 10:36
1
DebanjanB, I created a new thread here regarding remaining problems, please join if you want stackoverflow.com/questions/53300695/…
– Wonderio619
Nov 14 at 12:53
add a comment |
As per your question update to retrieve the books author name you need to induce WebDriverWait for the desired elements to be visible and you can use either of the following solutions:
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());
As per your question update to retrieve the books author name you need to induce WebDriverWait for the desired elements to be visible and you can use either of the following solutions:
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());
answered Nov 14 at 9:11
DebanjanB
38.3k73475
38.3k73475
1
DebanjanB, it really works ! I knew there must be some way to handle this, thank you )) I will accept your answer, but may be you can help me with Best Seller check also. I updated main post with the way I handled isBestSeller check, it works, but took too much time for check like this, may be a better way to handle this check exists ...
– Wonderio619
Nov 14 at 10:32
1
@Wonderio619 Glad to be able to help you !!! Can you please raise a new question with your new requirement? As there are multiple Answers available so changing the question will make the other answers invalid and may not be useful to future readers. For the time being I am reverting back your question to the previous state. StackOverflow contributers will be happy to help you out with your new question.
– DebanjanB
Nov 14 at 10:36
1
DebanjanB, I created a new thread here regarding remaining problems, please join if you want stackoverflow.com/questions/53300695/…
– Wonderio619
Nov 14 at 12:53
add a comment |
1
DebanjanB, it really works ! I knew there must be some way to handle this, thank you )) I will accept your answer, but may be you can help me with Best Seller check also. I updated main post with the way I handled isBestSeller check, it works, but took too much time for check like this, may be a better way to handle this check exists ...
– Wonderio619
Nov 14 at 10:32
1
@Wonderio619 Glad to be able to help you !!! Can you please raise a new question with your new requirement? As there are multiple Answers available so changing the question will make the other answers invalid and may not be useful to future readers. For the time being I am reverting back your question to the previous state. StackOverflow contributers will be happy to help you out with your new question.
– DebanjanB
Nov 14 at 10:36
1
DebanjanB, I created a new thread here regarding remaining problems, please join if you want stackoverflow.com/questions/53300695/…
– Wonderio619
Nov 14 at 12:53
1
1
DebanjanB, it really works ! I knew there must be some way to handle this, thank you )) I will accept your answer, but may be you can help me with Best Seller check also. I updated main post with the way I handled isBestSeller check, it works, but took too much time for check like this, may be a better way to handle this check exists ...
– Wonderio619
Nov 14 at 10:32
DebanjanB, it really works ! I knew there must be some way to handle this, thank you )) I will accept your answer, but may be you can help me with Best Seller check also. I updated main post with the way I handled isBestSeller check, it works, but took too much time for check like this, may be a better way to handle this check exists ...
– Wonderio619
Nov 14 at 10:32
1
1
@Wonderio619 Glad to be able to help you !!! Can you please raise a new question with your new requirement? As there are multiple Answers available so changing the question will make the other answers invalid and may not be useful to future readers. For the time being I am reverting back your question to the previous state. StackOverflow contributers will be happy to help you out with your new question.
– DebanjanB
Nov 14 at 10:36
@Wonderio619 Glad to be able to help you !!! Can you please raise a new question with your new requirement? As there are multiple Answers available so changing the question will make the other answers invalid and may not be useful to future readers. For the time being I am reverting back your question to the previous state. StackOverflow contributers will be happy to help you out with your new question.
– DebanjanB
Nov 14 at 10:36
1
1
DebanjanB, I created a new thread here regarding remaining problems, please join if you want stackoverflow.com/questions/53300695/…
– Wonderio619
Nov 14 at 12:53
DebanjanB, I created a new thread here regarding remaining problems, please join if you want stackoverflow.com/questions/53300695/…
– Wonderio619
Nov 14 at 12:53
add a comment |
Well, from the URL I am able to extract the info.
@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());
}
}
and here is the console output with the author name:
[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
Code for updated question:
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());
}
}
Dhamo, I got your point, but this is not exactly what I'm trying to do. I need to extract all authors names from search page. Not find author name by checking href. And you need to access href section and manually type author name for each search result for your code to work, this is not the way I want it to work. I updated main post with my code, I think you will understand what I'm trying to do.
– Wonderio619
Nov 13 at 16:30
1
Well, in that case, to extract all author names in the page, you need to change [to xpath: //span[@class='a-size-small a-color-secondary' and contains(.,'by')]/../span[2]] the locator as the one you have is not only locating the author names but locating many elements in the page.
– Dhamo
Nov 13 at 18:15
Dhamo, thank you, I will test your code tomorrow on internship, because my internet connection is very bad now. I see you used xpath for locating authors, is it somehow generated, or you write it using your own skills ? It is strange for me that I was not able to access simple linkText value from href by using cssselector, may be I should start learning xpath ...
– Wonderio619
Nov 13 at 19:55
1
I wrote it. Learning xpath is must. All the best!
– Dhamo
Nov 13 at 20:01
1
@Wonderio619 - Please check the new code now . It is working.
– Dhamo
Nov 14 at 18:01
|
show 2 more comments
Well, from the URL I am able to extract the info.
@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());
}
}
and here is the console output with the author name:
[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
Code for updated question:
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());
}
}
Dhamo, I got your point, but this is not exactly what I'm trying to do. I need to extract all authors names from search page. Not find author name by checking href. And you need to access href section and manually type author name for each search result for your code to work, this is not the way I want it to work. I updated main post with my code, I think you will understand what I'm trying to do.
– Wonderio619
Nov 13 at 16:30
1
Well, in that case, to extract all author names in the page, you need to change [to xpath: //span[@class='a-size-small a-color-secondary' and contains(.,'by')]/../span[2]] the locator as the one you have is not only locating the author names but locating many elements in the page.
– Dhamo
Nov 13 at 18:15
Dhamo, thank you, I will test your code tomorrow on internship, because my internet connection is very bad now. I see you used xpath for locating authors, is it somehow generated, or you write it using your own skills ? It is strange for me that I was not able to access simple linkText value from href by using cssselector, may be I should start learning xpath ...
– Wonderio619
Nov 13 at 19:55
1
I wrote it. Learning xpath is must. All the best!
– Dhamo
Nov 13 at 20:01
1
@Wonderio619 - Please check the new code now . It is working.
– Dhamo
Nov 14 at 18:01
|
show 2 more comments
Well, from the URL I am able to extract the info.
@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());
}
}
and here is the console output with the author name:
[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
Code for updated question:
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());
}
}
Well, from the URL I am able to extract the info.
@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());
}
}
and here is the console output with the author name:
[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
Code for updated question:
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());
}
}
edited Nov 14 at 18:00
answered Nov 13 at 16:21
Dhamo
8116
8116
Dhamo, I got your point, but this is not exactly what I'm trying to do. I need to extract all authors names from search page. Not find author name by checking href. And you need to access href section and manually type author name for each search result for your code to work, this is not the way I want it to work. I updated main post with my code, I think you will understand what I'm trying to do.
– Wonderio619
Nov 13 at 16:30
1
Well, in that case, to extract all author names in the page, you need to change [to xpath: //span[@class='a-size-small a-color-secondary' and contains(.,'by')]/../span[2]] the locator as the one you have is not only locating the author names but locating many elements in the page.
– Dhamo
Nov 13 at 18:15
Dhamo, thank you, I will test your code tomorrow on internship, because my internet connection is very bad now. I see you used xpath for locating authors, is it somehow generated, or you write it using your own skills ? It is strange for me that I was not able to access simple linkText value from href by using cssselector, may be I should start learning xpath ...
– Wonderio619
Nov 13 at 19:55
1
I wrote it. Learning xpath is must. All the best!
– Dhamo
Nov 13 at 20:01
1
@Wonderio619 - Please check the new code now . It is working.
– Dhamo
Nov 14 at 18:01
|
show 2 more comments
Dhamo, I got your point, but this is not exactly what I'm trying to do. I need to extract all authors names from search page. Not find author name by checking href. And you need to access href section and manually type author name for each search result for your code to work, this is not the way I want it to work. I updated main post with my code, I think you will understand what I'm trying to do.
– Wonderio619
Nov 13 at 16:30
1
Well, in that case, to extract all author names in the page, you need to change [to xpath: //span[@class='a-size-small a-color-secondary' and contains(.,'by')]/../span[2]] the locator as the one you have is not only locating the author names but locating many elements in the page.
– Dhamo
Nov 13 at 18:15
Dhamo, thank you, I will test your code tomorrow on internship, because my internet connection is very bad now. I see you used xpath for locating authors, is it somehow generated, or you write it using your own skills ? It is strange for me that I was not able to access simple linkText value from href by using cssselector, may be I should start learning xpath ...
– Wonderio619
Nov 13 at 19:55
1
I wrote it. Learning xpath is must. All the best!
– Dhamo
Nov 13 at 20:01
1
@Wonderio619 - Please check the new code now . It is working.
– Dhamo
Nov 14 at 18:01
Dhamo, I got your point, but this is not exactly what I'm trying to do. I need to extract all authors names from search page. Not find author name by checking href. And you need to access href section and manually type author name for each search result for your code to work, this is not the way I want it to work. I updated main post with my code, I think you will understand what I'm trying to do.
– Wonderio619
Nov 13 at 16:30
Dhamo, I got your point, but this is not exactly what I'm trying to do. I need to extract all authors names from search page. Not find author name by checking href. And you need to access href section and manually type author name for each search result for your code to work, this is not the way I want it to work. I updated main post with my code, I think you will understand what I'm trying to do.
– Wonderio619
Nov 13 at 16:30
1
1
Well, in that case, to extract all author names in the page, you need to change [to xpath: //span[@class='a-size-small a-color-secondary' and contains(.,'by')]/../span[2]] the locator as the one you have is not only locating the author names but locating many elements in the page.
– Dhamo
Nov 13 at 18:15
Well, in that case, to extract all author names in the page, you need to change [to xpath: //span[@class='a-size-small a-color-secondary' and contains(.,'by')]/../span[2]] the locator as the one you have is not only locating the author names but locating many elements in the page.
– Dhamo
Nov 13 at 18:15
Dhamo, thank you, I will test your code tomorrow on internship, because my internet connection is very bad now. I see you used xpath for locating authors, is it somehow generated, or you write it using your own skills ? It is strange for me that I was not able to access simple linkText value from href by using cssselector, may be I should start learning xpath ...
– Wonderio619
Nov 13 at 19:55
Dhamo, thank you, I will test your code tomorrow on internship, because my internet connection is very bad now. I see you used xpath for locating authors, is it somehow generated, or you write it using your own skills ? It is strange for me that I was not able to access simple linkText value from href by using cssselector, may be I should start learning xpath ...
– Wonderio619
Nov 13 at 19:55
1
1
I wrote it. Learning xpath is must. All the best!
– Dhamo
Nov 13 at 20:01
I wrote it. Learning xpath is must. All the best!
– Dhamo
Nov 13 at 20:01
1
1
@Wonderio619 - Please check the new code now . It is working.
– Dhamo
Nov 14 at 18:01
@Wonderio619 - Please check the new code now . It is working.
– Dhamo
Nov 14 at 18:01
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283467%2fis-it-possible-to-get-books-author-name-value-from-href-tag-with-selenium-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Try using getAttribute("value")
– Dhamo
Nov 13 at 15:44
The output is null, not working.
– Wonderio619
Nov 13 at 15:50
1
Have you tried with other locators? Eg: xpath: //a[contains(@href,'/Cay-S.-Horstmann/')]' also check there are no frames exist.
– Dhamo
Nov 13 at 15:53
No luck. To be more specific, I'm trying to get books author name from searched books page on amazon.com, for example you can visit this link with search querry "Java "amazon.com/s/… , I tried to get author name from there. And what you mean by frames ?
– Wonderio619
Nov 13 at 16:01
1
Well, I am able to extract the author names from the url. I don't see a problem with the locator.
– Dhamo
Nov 13 at 16:17