zac
zac

Reputation: 4918

How to send a click to hidden button?

I have a simple submit button on a page that I need to send a click for it but this button is at the bottom of the page and does not appear unless a user uses the browser vertical scroll bar to reach it !!

so when I use this C# code I get error element is not visible so how I can solve this problem ?

driver.FindElement(By.Id("submit")).Click();

Upvotes: 0

Views: 537

Answers (4)

zac
zac

Reputation: 4918

I found the problem there are two elements in the page with id="submit" !! I used Name and now it work fine, thank to all.

Upvotes: 0

Mahipal
Mahipal

Reputation: 910

Selenium Webdriver implicitly scroll to the element, if it is visible. You can wait for the visibility of the element and then perform the click operation. In Java, we can wait for the visibility using following code:

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(driver.findElement(By.id("submit"))));

In the above code, Webdriver will wait for 60 seconds, for the required element to become visible. If within 60 seconds, the element does not appear, then it will fail with timeout error. Once, the required element is visible, it can be clicked.

Upvotes: 1

acikojevic
acikojevic

Reputation: 945

You could scroll down using javascript. Something like this should do the work.

((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");

Upvotes: 1

Jeremy L
Jeremy L

Reputation: 321

If it's not visible by you, it is not render by the browser used through your selenium driver.
I recommend you to simulate a scrolling to ensure your item will be visible, or do a simulation on a bigger resolution ?
Eventually you can consider switch to an headless driver...

Upvotes: 0

Related Questions