- 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 clickandwait in Selenium Webdriver using Java?
We can click and wait in Selenium. This can be achieved by the synchronization concept. We shall use the explicit wait condition and wait for an element to be clickable prior to the next step.
The explicit wait waits for a specified amount of time before throwing an exception. To verify if an element is clickable, we can use the expected condition elementToBeClickable.
Example
Code Implementation.
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.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class ElementClickableWait{ 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); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); // identify element and click() WebElement l=driver.findElement(By.className("mui-btn")); l.click(); // explicit wait condition WebDriverWait w = new WebDriverWait(driver,3); // elementToBeClickable condition w.until(ExpectedConditions .elementToBeClickable (By.className("s-buy"))); // get page title of next page System.out.println("Current page title:" + driver.getTitle()); driver.quit(); } }
Output
Advertisements