Selenium - Search multiple keywords in Google after each other
Java - Selenium - Search multiple keywords in Google after each other.
I actually want to perform a search on Google with Selenium (or a search field on a website), whereas the input is a list of keywords say:
I want to search on Google:
- Keyword: a
- then Keyword: b
- then Keyword: n
- then Keyword: x
How do I actually automate this? I cannot find anything useful.. I saw something about dictionaries which you can set up, but not in practice.
So Search Google -> for a list of keywords (a, b, n, x)
Any help would be awesome!
what i did sofar:
import org.openqa.selenium.*;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.io.FileHandler;import java.io.File;import java.io.IOException;import java.util.List;public class GoogleSearch{ static WebDriver driver; public static void main(String[] args) throws IOException, InterruptedException { System.setProperty("webdriver.chrome.driver","C:\\Users\\xx\\Software\\selenium_browser_drivers\\Chrome86\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://google.com/"); Thread.sleep(10000); System.out.println(driver.getTitle()); driver.findElement(By.xpath("//input[@name='q']")).click(); Thread.sleep(2000); driver.findElement(By.xpath("//input[@name='q']")).sendKeys("My Search Text"); Thread.sleep(2000); driver.findElement(By.xpath("//input[@name='q']")).sendKeys(Keys.DOWN); Thread.sleep(2000); driver.findElement(By.xpath("//input[@name='q']")).sendKeys(Keys.ENTER); Thread.sleep(2000); List<WebElement> links = driver.findElements(By.xpath("//*[@id='res']//a/h3")); int count = links.size(); System.out.println(count); for (int i =0;i<count;i++) { String linkname = driver.findElements(By.xpath("//*[@id='res']//a/h3")).get(i).getText(); System.out.println(linkname); } getScreenshot(); } public static void getScreenshot() throws IOException { File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileHandler.copy(src,new File("C:\\Users\\xx\\Desktop\\google.png")); }}
