Reputation: 25
I am doing a project where I have to compare the Google search results with another search results engine. I am planning to perform this testing by comparing the search results using Selenium tool.
What I want to do is:
Now i got a code from stackoverflow site as below. Modified to my requirement. It is fetching the first result's title and link. How do i get the title and link of the entire page?
My code is:
package gooPkg;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class testSearch {
public static void main(String[] args) {
String baseUrl = "https://www.google.co.in/";
WebDriver driver = new FirefoxDriver();
//String underConsTitle = "Under Construction: Mercury Tours";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl);
// Enter the query string "Cheese"
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("obama twitter");
query.submit();
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
WebElement resultsDiv = driver.findElement(By.className("gssb_e"));
if (resultsDiv.isDisplayed()) {
break;
}
}
List<WebElement> weblinks = driver.findElements(By.xpath("/html/body/div/div[3]/div[2]/div[6]/div/div[4]/div/div[2]/div[2]/div/ol/li/div/div/h3/a"));
for (WebElement suggestion : weblinks) {
System.out.println(suggestion.getText()+"\n");
System.out.println("==> "+suggestion.getAttribute("href")+"\n");
}
}}
Upvotes: 2
Views: 4896
Reputation: 5667
You can use findElements to get the all the search result titles & links.
String baseUrl = "https://www.google.co.in/";
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl);
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("obama twitter");
query.submit();
//==============Here logic goes =======================
//get all the search result
List<WebElement> allSearchResults=driver.findElements(By.cssSelector("ol li h3>a"));
//iterate the above list to get all the search titles & links from that page
for(WebElement eachResult : allSearchResults) {
System.out.println("Title : "+eachResult.getText()+", Link : "+eachResult.getAttribute("href"));
}
EDIT-I
Try with below logic. Include search term in url itself.
String baseUrl = "https://www.google.co.in/#q=webdriver";
driver.get(baseUrl);
//get all the search result
List<WebElement> allSearchResults=driver.findElements(By.cssSelector("ol li h3>a"));
//iterate the above list to get all the search titles & links from that page
for(WebElement eachResult : allSearchResults) {
System.out.println("Title : "+eachResult.getText()+", Link : "+eachResult.getAttribute("href"));
}
Upvotes: 1