Created
April 6, 2016 09:05
-
-
Save vvavrychuk/23d887fd30a479def327f3bb8efbec8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import org.openqa.selenium.support.ui.ExpectedConditions; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.List; | |
public class GoogleInfiniteSearch { | |
static void printResults(WebDriver driver) { | |
WebDriverWait wait = new WebDriverWait(driver, WebDriverWait.DEFAULT_SLEEP_TIMEOUT); | |
WebElement resultsElement = | |
wait.until(ExpectedConditions.presenceOfElementLocated(By.className("srg"))); | |
List<WebElement> results = resultsElement.findElements(By.className("g")); | |
for (WebElement result : results) { | |
System.out.println(result.findElement(By.tagName("h3")).findElement(By.tagName("a")).getText()); | |
} | |
} | |
public static void main(String[] args) throws IOException, InterruptedException { | |
System.out.print("Please enter query: "); | |
String query = new BufferedReader(new InputStreamReader(System.in)).readLine(); | |
WebDriver driver = new ChromeDriver(); | |
WebDriverWait wait = new WebDriverWait(driver, WebDriverWait.DEFAULT_SLEEP_TIMEOUT); | |
driver.get("http://google.com"); | |
driver.findElement(By.tagName("body")).sendKeys(query); | |
WebElement button = driver.findElement(By.id("sblsbb")).findElement(By.tagName("button")); | |
button = wait.until(ExpectedConditions.elementToBeClickable(button)); | |
button.click(); | |
printResults(driver); | |
int pageNumber = 1; | |
while (true) { | |
pageNumber++; | |
WebElement link = driver.findElement(By.xpath("//a[@aria-label='Page " + pageNumber + "']")); | |
link.click(); | |
Thread.sleep(1000); // After click old results are present but soon they will disappear and new one will be rendered. | |
printResults(driver); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment