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,950 points
23,748 views
how to avoid the pop-up window in chrome browser.

when every i run my script it's open the chrome browser it'showing pop-up like.

browser or delete search activity data.

it's displaying the two buttons like "no thanks" and "show me" buttons .

this my code please any solution help me to overcome the problem.

public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "C:\\softwares\\selenium\\webdrivers\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

driver.get("http://google.com");

driver.manage().window().maximize();

driver.findElement(By.className("gb_1 gb_nd gb_od")).click();

Thread.sleep(3000);

System.out.println(driver.getTitle());

driver.findElement(By.id("fakebox-input")).sendKeys("facebook");
Hi @Naresh, please checkout this link. I have created a new thread for your problem, so that it can be properly seen and answered by others: https://www.edureka.co/community/55645/how-to-avoid-the-pop-up-window-in-chrome-browser-with-selenium

1 answer to this question.

+2 votes
Best answer

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

selected Aug 26, 2019 by Abha

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

How can I automate google search for a keyword using ruby selenium webdriver?

Hi Amir, following code snippet shows how ...READ MORE

answered Aug 26, 2019 in Selenium by Anvi
• 14,150 points
1,599 views
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,510 points
1,938 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,686 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,149 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,228 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,880 views
0 votes
1 answer

How to print all google suggestions for a keyword using Selenium Webdriver?

Hello Cherry, below is the automation script ...READ MORE

answered May 23, 2019 in Selenium by Abha
• 28,140 points
5,299 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,480 points
44,651 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