-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vezeeta_Search.java
129 lines (99 loc) · 3.54 KB
/
Vezeeta_Search.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package test_search;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Vezeeta_Search {
private static String SEARCH_INPUT_BAR ="q";
//private static String SEARCH_RESULT_BLOCK ="srg";
private static String SEARCH_RESULT_HITS ="rc";
private static String RESULT_TITLE ="h3.LC20lb";
private static String RESULT_SECTION="span.st";
private static String FEATURED_SECTION="ILfuVd";
private static ChromeDriver driver = null;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/home/testing/chromedriver_linux64/chromedriver");
driver = new ChromeDriver();
loadWebsite("https://www.google.com");
performGoogleSearch("programming");
ArrayList<SearchBlock> results1 = parsSearchResults();
goToNextPage();
ArrayList<SearchBlock> results2 = parsSearchResults();
ArrayList<SearchBlock> combinedResults = new ArrayList();
combinedResults.addAll(results1);
combinedResults.addAll(results2);
printResults(combinedResults);
}
//Sleeps for given number of seconds
private static void sleep(long seconds) {
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Loads given website
private static void loadWebsite(String site) {
driver.get(site);
sleep(1);
}
//Performs google search for given query
private static void performGoogleSearch(String query) {
WebElement searchInput =driver.findElementByName(SEARCH_INPUT_BAR);
searchInput.sendKeys(query);
searchInput.sendKeys(Keys.RETURN);
sleep(1);
}
/*Parse search results and return a list of title/sections.
* Also finds featured section if it exists
*/
private static ArrayList<SearchBlock> parsSearchResults(){
ArrayList<SearchBlock> results =new ArrayList<>();
String featuredSectionText ="";
try {
WebElement featuredSection = driver.findElementByClassName(FEATURED_SECTION);
featuredSectionText = featuredSection.getText();
} catch (NoSuchElementException ignored) {}
List<WebElement> hits = driver.findElements(By.className(SEARCH_RESULT_HITS));
for(WebElement hit: hits) {
String title = hit.findElement(By.cssSelector(RESULT_TITLE)).getText();
String section = hit.findElement(By.cssSelector(RESULT_SECTION)).getText();
if (!featuredSectionText.equals("")) {
results.add(new SearchBlock(title, featuredSectionText));
featuredSectionText ="";
} else if (!title.equals("")) {
results.add(new SearchBlock(title, section));
}
}
return results;
}
//Goes to next page
private static void goToNextPage() {
int nextPageBtnIndex = driver.findElementsByClassName("pn").size() - 1;
WebElement nextPageButton = driver.findElementsByClassName("pn")
.get(nextPageBtnIndex);
nextPageButton.click();
sleep(2);
}
//Prints google search results by title/section
private static void printResults(ArrayList<SearchBlock> searchResults) {
for(SearchBlock block: searchResults) {
System.out.println("Title:" + block.title);
System.out.println("Section:" + block.section);
System.out.println();
}
}
//Class to hold title/section
private static class SearchBlock{
String title;
String section;
SearchBlock(String title, String section){
this.title = title;
this.section = section;
}
}
}