How to search for a keyword in google using selenium Java?

+2 votes
I am new to selenium and I have started from ground level and working with chrome and I am able to open google. Now my question is how to search for a keyword there?
Dec 17, 2018 in Selenium by Anjali
• 2,900 points
1,819 views

1 answer to this question.

+1 vote

You can use locators to find the textbox and then search for a keyword you like. 

1. Open Google and right click to inspect element. You can use F12 also to inspect elements.

 

2. Go to the searchbox and inspect the web element you can use for your purpose.

You can see that there is a name field, I will use it for finding the search box.

Here is the code that I am using:

System.setProperty("webdriver.chrome.driver","C:\\Users\\Downloads\\chromedriver.exe");
WebDriver wd = new ChromeDriver();//create an instance for webdriver named wd of type chrome
wd.get("https://www.google.com");//open google
WebElement elem = wd.findElement(By.name("q"));//finding the web element using name locator
elem.sendKeys(new String[]{"selenium is fun"});
elem.submit();

Output:

answered Dec 17, 2018 by Nabarupa

Related Questions In Selenium

0 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
145 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,070 points
443 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
792 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
433 views
0 votes
1 answer
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
1,769 views