- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use Selenium webdriver to click google search?
We can click on Google search with Selenium webdriver. First of all we need to identify the search edit box with help of any of the locators like id, class,name, xpath or css.
Then we will input some text with the sendKeys() method. Next we have to identify the search button with help of any of the locators like id, class, name, xpath or css and finally apply click() method on it or directly apply submit() method. We will wait for the search results to appear with presenceOfElementLocatedexpected condition.
We need to import org.openqa.selenium.support.ui.ExpectedConditions and import org.openqa.selenium.support.ui.WebDriverWait to incorporate expected conditions and WebDriverWait class. This concept comes from the explicit wait condition in synchronization.
Let us try to implement the below scenario.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; public class SearchAction{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.google.com/"); // identify element WebElement p=driver.findElement(By.name("q")); //enter text with sendKeys() then apply submit() p.sendKeys("Selenium Java"); // Explicit wait condition for search results WebDriverWait w = new WebDriverWait(driver, 5); w.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul"))); p.submit(); driver.close(); } }
Output
- Related Articles
- How to click on across browsers using Selenium Webdriver?
- How to click on hidden element in Selenium WebDriver?
- How to click on image in selenium webdriver Python?
- Selenium Webdriver submit() vs click().
- How to click Allow on Show Notifications popup using Selenium Webdriver?
- How to click on a link using Selenium webdriver in Python.
- How to use Selenium WebDriver for Web Automation?
- How to use JavaScript in Selenium to click an Element?
- How to search for a keyword in google using Selenium Java?
- Google Search Automation with Python Selenium
- How to use clickandwait in Selenium Webdriver using Java?
- How to use a click() method in Selenium with python?
- How to use xPath in Selenium WebDriver to grab SVG elements?
- How to set Google Chrome in WebDriver?
- How to use chrome webdriver in Selenium to download files in Python?
