1

Selenium click doesn't seem to work unless the element is visible. So I want bring element to view before click. There is a focus method in Selenium, but it does not seem to scroll the view. I tried Amith solution for Scroll Element into View with Selenium but some times element remained under navbar. Is there any way for scroll until element become observable and selenium can click on it?

ali-myousefi
  • 822
  • 2
  • 11
  • 26
  • 2
    What have you tried to achieve your wanted results? What has your research concerning your problem shown? Can you provide code of your tries? [How do I ask a good question](https://stackoverflow.com/help/how-to-ask), [How much research effort is expected](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) might be helpful to improve your question. – Geshode Jan 13 '18 at 07:30

2 Answers2

2

I found a solution by using JavaScript

public static void Scroll(this IWebElement elem)
{
     IJavaScriptExecutor js = (IJavaScriptExecutor)driver;     
     js.ExecuteScript("window.scroll(arguments[0], arguments[1]);", elem.Location.X, elem.Location.Y - heightOfNavbar);
}
ali-myousefi
  • 822
  • 2
  • 11
  • 26
0

You could try using JavaScript, not sure that it'll work but you can give it a try:

private void ScrollToElement(IWebElement element)
{
    IJavaScriptExecutor js = (IJavaScriptExecutor) driver; 
    js.executeScript("argumenents[0].scrollIntoView(true);", element)
}

I adapted it to C# from a similar issue I ran into a long time ago. At the time I was working in Java and this solution worked for me. Hopefully it helps you or at least points you in the right direction.

so cal cheesehead
  • 2,515
  • 4
  • 29
  • 50