Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (140 points)

Hello,

I want to select option in google search and hit enter to get the results via python selenium.

I opened the google and send keys for the value 'Software Testing'

I am not able to select different options and hit enter to get the results.

I use

MySearch =driver.find_element_by_xpath("//input[@name='q']")
MySearch.send_keys("Software testing")
MySearch.send_keys(Keys.ENTER)

I tried with Keys.RETURN but getting an error as 'Undefined Variable from Import : RETURN.

Please guide.

Thanks !

1 Answer

0 votes
by (62.9k points)

Here's a solution which works, comment down below if you want to know something more on this! 

public class GoogleSearch {

    public static void main(String[] args) {

        WebDriver driver=new ChromeDriver(); 

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

/*get method of WebDriver class will navigate you to the specified site*/

                         driver.findElement(By.xpath("//input[@type='text']")).sendKeys("Software Testing");

/*findElement of the WebDriver class will be to used to locate the webelement(here, Search box ) by xpath(located from chropath by inspecting), and the sendKeys method for typing the mentioned text in the search box.*/   

            driver.findElement(By.xpath("//button[@name='btnG']")).click();

/* findElement method of WebDriver class to locate the Submit button element by xpath and click method to click that button.*/

        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 

/*This specifies the amount of time the driver should wait when searching for an element if it is not immediately present.*/

        driver.findElement(By.xpath("(//h3[@class='r']/a)[3]")).click(); 

/* thid parameter in the xpathmethod will choose the 3rd option from the search menu.*/

        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 

/*This specifies the amount of time the driver should wait when searching for an element(here the 3rd search for to get loaded and displayed) if it is not immediately present.*/

    }

}

Comments with respect to the commands are written below to it.

Hope this helps!

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium certification!

by (140 points)
Thanks ! It worked.

Browse Categories

...