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,760 points
2,357 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 29, 2018 in Selenium by bug_seeker
• 15,520 points
1,877 views
0 votes
5 answers

How to scroll down a webpage in selenium using Java?

 to scroll up or down with Selenium, a JavaScript executor is a ...READ MORE

answered Dec 16, 2020 in Selenium by Roshni
• 10,520 points
44,534 views
+1 vote
1 answer

How to get the HTML source of a webpage using Selenium in Java?

There is a method called getPageSource() in ...READ MORE

answered Jan 11, 2019 in Selenium by Sneha
29,255 views
0 votes
2 answers

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,500 points
13,457 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,019 views
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,910 points
7,133 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,810 views
0 votes
1 answer
+10 votes
17 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,360 points
197,365 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP