roni1800
roni1800

Reputation: 117

Can't move to a webelement?

Obviously selenium doesn't interact with an webelement until it's in the view, and selenium automatically tries to scroll to that webelement. But in my case when I try to click a particular button it doesn't scroll it in to view, it simply scrolls to a random place in the page.

Objective: scrolls the webelement into view, and then click that element.

Methods I've already used:

element.Click(); //method 1

Actions actions = new Actions(driver); // method 2
actions.MoveToElement(element);
actions.Perform();

IJavaScriptExecutor js = driver as IJavaScriptExecutor; //method 3
js.ExecuteScript("$('#Id_Body' + element_id)[0].scrollIntoView( true );"); //because the driver scrolls to a random place I use this to get back to the top of the page.
int Y = element.Location.Y, X = element.Location.X;
js.ExecuteScrip($"window.scrollBy( {X}, {Y};");

I'm using selenium 2.48.0, firefoxDriver 43.0.1

Is there a fix for this issue? if someone knows of an older version of selenium/firefox that works fine with one of these methods please tell me, thanks.

Upvotes: 0

Views: 832

Answers (2)

Guy
Guy

Reputation: 50809

Try double MoveToElement

Actions action = new Actions(driver);
action.MoveToElement(elementParent).MoveToElement(elementToClick).Build().Perform();

First move to the element area and than to the element you want to click.

Upvotes: 2

alecxe
alecxe

Reputation: 473813

Why don't use scrollIntoView() directly:

js.ExecuteScript("arguments[0].scrollIntoView();", element); 

Upvotes: 0

Related Questions