Skip to content

Commit 0cfd762

Browse files
author
Carter Hollman
committed
First working implementation of handleSearch, works with Indeed
1 parent 9cb53b0 commit 0cfd762

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/main/search/handleSearch.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import org.openqa.selenium.By;
2+
import org.openqa.selenium.WebElement;
3+
import org.openqa.selenium.WebDriver;
4+
import org.openqa.selenium.chrome.ChromeDriver;
5+
import java.time.Duration;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class handleSearch{
10+
public static ArrayList<String> handleSearch(String link, String criteria){
11+
//New WebDriver with implicit wait of 5 seconds if elements aren't found on page
12+
WebDriver driver = new ChromeDriver();
13+
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
14+
15+
//Setup page and declare search variable cuz scope and stuff
16+
driver.get(link);
17+
WebElement searchField;
18+
19+
20+
//Try to find the search field via XPATH, to add functionality for more websites can add their specific XPATH to
21+
//try block via concatenation of the following string " | <xpath>"
22+
try{
23+
searchField = driver.findElement(By.xpath(
24+
"//input[@type='text' or @type='search' or @placeholder[contains(., 'Search')]]"
25+
));
26+
}catch(Exception e){
27+
return null;
28+
}
29+
30+
//Since we found a search field, clear it, input criteria, submit to navigate to next page
31+
searchField.clear();
32+
searchField.sendKeys(criteria);
33+
searchField.submit();
34+
35+
36+
37+
//Should now be on page where we can scrape for the links
38+
ArrayList<String> links = new ArrayList<>();
39+
40+
41+
//Loop is for changing pages if necessary
42+
boolean next = true;
43+
while(next) {
44+
45+
//If we don't find a next button then we break the loop of changing pages
46+
WebElement nextButton = null;
47+
try{
48+
nextButton = driver.findElement(By.xpath("//a[@data-testid='pagination-page-next']"));
49+
}catch(Exception e){
50+
next = false;
51+
}
52+
53+
54+
55+
//Isolate individual result elements into a list to iterate over
56+
List<WebElement> results = driver.findElements(By.xpath("//li[@class='css-1ac2h1w eu4oa1w0']"));
57+
58+
//For every individual result, find its anchor tag and extract the href attribute.
59+
//That attribute holds the link we are looking for, so we add to outgoing list :)
60+
//NOTE: Some elements in the results do not have link or are filler try/catch ignores them
61+
for (WebElement result : results) {
62+
try {
63+
String temp = result.findElement(By.tagName("a")).getAttribute("href");
64+
links.add(temp);
65+
} catch (Exception ignored) {
66+
67+
}
68+
}
69+
70+
//Continue through the pages :)
71+
if(nextButton != null)
72+
driver.navigate().to(nextButton.getAttribute("href"));
73+
}
74+
75+
//All done, success!!!!!
76+
driver.quit();
77+
return links;
78+
}
79+
}
80+

0 commit comments

Comments
 (0)