How to search for multiple keywords in a random manner using Selenium in Java?

0 votes
I have an array of key words that I want to search in a random way in selenium in google.

Can someone help me with this task of mines?
Dec 28, 2018 in Selenium by Sradha
• 1,720 points
128 views

1 answer to this question.

0 votes

Hey @Sradha, 

As you have created an array of keywords and you want to search it in a random way. You can use normal script for searching in google and induce your array and pass your elements in a random manner using .random function. Again you can create an array of index of those key words. That's all.

Here is a code for doing that:-

import java.util.Random;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SerachRandom 
{
    public static void main(String[] args) throws InterruptedException 
    {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\Nabarupa\\chromedriver.exe");
        String str[] = {"Hello World", 
        "India","Earth","Asia","Community","Selenium"}
        try{
            WebDriver driver=new ChromeDriver();
            driver.get("https://www.google.com");
            int j[] = {0,1,2,3,4,5};
            Random generator = new Random();
            int randomIndex = generator.nextInt(j.length);
            int k = j[randomIndex];
            driver.findElement(By.name("q")).sendKeys(str[k],Keys.ENTER);
            Thread.sleep(5000);
            driver.close();
            }
       catch(Exception e)
       {
            System.out.println(e);
       }
    }
}

Hope this helps. 

answered Dec 28, 2018 by Nabarupa

Related Questions In Selenium

+2 votes
1 answer
0 votes
1 answer

How to wait for the webpage to load in selenium using java

Firstly, when you launch a web application, ...READ MORE

answered Aug 28, 2018 in Selenium by bug_seeker
• 15,350 points
169 views
0 votes
2 answers

How to scroll down a webpage in selenium using Java?

This works... JavascriptExecutor jse = (JavascriptExecutor)driver; j ...READ MORE

answered Apr 3 in Selenium by anonymous
3,031 views
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
576 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
898 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
565 views
+9 votes
14 answers

How to automate gmail login process using selenium webdriver in java?

Check the below code: Here is the working ...READ MORE

answered Apr 24, 2018 in Selenium by Vardy
• 2,250 points
59,419 views