1
  • As a user
  • When I go to a page
  • Then there is an element that scrolls down into view

There is a link I want to click on that element. Sometimes it will work but how do I wait for it to finish scrolling down and then click on the element?

Thread.sleep(1000) works but I want to know is there a better approach?

Tools I am using:

Java
Selenium 
1
  • 2
    This scroll is performed by Selenium or how? Can you add some code to see how you try to perform click on element? Commented Feb 14, 2019 at 11:10

1 Answer 1

2

Use webdriver wait until the element visible or click able.Try the below code.

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Id here"))).click();

Or

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("Id here"))).click();

Please Note: Instead of ID you can put any locator like xpath,class,CSS.I have provided one example of ID here.

Sign up to request clarification or add additional context in comments.

4 Comments

this is the best practice for selenium!
Please @Kajal Don't answer the question which are already answered you can mark duplicate reputation doesn't matter
Please make sure that next time, otherwise their are peoples who are over-smart and down vote you, they don't see the efforts had you made to wrote the answer..
Sure mate.I have noted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.