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.
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.
Why don't use scrollIntoView() directly:
js.ExecuteScript("arguments[0].scrollIntoView();", element);
Related
I am getting the following error while trying to click on a button which is present at the bottom of the page. Looks like this error is coming because that button is not visible at the first point, if we scroll the page down then only selenium is able to identify that button.
OpenQA.Selenium.ElementClickInterceptedException : element click intercepted: Element <span>...</span> is not clickable at point (1113, 659)..
I have tried with following code to scroll down the web page but it does not help me.
Actions actions = new Actions(driver);
actions.SendKeys(Keys.ArrowDown);
If I try with element.Sendkeys(Keys.ArrowDown) then also its not helping.
what would be the correct approach here?
You should use Actions class to perform scrolling to the element in this way
Actions actions = new Actions(driver);
var element = driver.FindElement(...);
actions.MoveToElement(element);
actions.Perform();
substitute ... with the selector for the element.
There is also another method relying on javascript
IJavaScriptExecutor js = (IJavaScriptExecutor) driver;
js.ExecuteScript("arguments[0].scrollIntoView();", element);
I use Selenium C# binding and I want to click over the addToCart button.
First, I'm waiting the button appears on the page with ExpectedConditions.ToBeClickable.
Then, I need to scroll down the page to be able to click over the button. I used the Selenium.Interactions class but it work as well with js executor.
private By addToCartBy = By.XPath("/html/body/div[2]/div/div/div[1]/div/div[3]/div[2]/div/section[1]/div[2]/aside/div/div[2]/div/div[2]/div/button");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
IWebElement addToCart = wait.Until(ExpectedConditions.ElementToBeClickable(addToCartBy));
Actions action = new Actions(driver);
action.MoveToElement(addToCart);
action.Perform();
addToCart.Click();
When I perform this test with Chrome driver, the page loads and the navigator scroll down to the element addToCart and click it but I'm unable to get the button working properly.
When I use Firefox driver, it raise this exception:
OpenQA.Selenium.WebDriverException : '(1435, 998) is out of bounds of viewport width (1920) and height (966)'and I'm unable to click over the button
Most of the time, the click will be performed but no action will follow from this. If I want the button to work properly (go the the cart), I need to put a System.Threading.Thread.Sleep(2000) to be effective or a Console.ReadLine(). Even if I disable the automation click and I do it manually, the button don't always proceed.
How can I make sure my element addToCart is displayed after I moved to Element? in a way to click it when he's ready.
Thank you, Eelke Johnson
For some of the scenarios, you can use not
IWebElement.Click();
but
IWebElement.SendKeys(Keys.Enter);
I had some scenarios, where just Click() didn't work for me.Maybe this will help in your situation.
You need to consider a couple of things.
To invoke MoveToElement(), ExpectedConditions as ElementToBeClickable() is overkill. Instead you can do away with ElementIsVisible().
While invoking Click() as per best practices you need to invoke WebDriverWait inconjunction with ExpectedConditions as ElementToBeClickable().
So effectively, your code block will be:
Actions action = new Actions(driver);
action.MoveToElement(new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.XPath("/html/body/div[2]/div/div/div[1]/div/div[3]/div[2]/div/section[1]/div[2]/aside/div/div[2]/div/div[2]/div/button")))).Perform();
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("/html/body/div[2]/div/div/div[1]/div/div[3]/div[2]/div/section[1]/div[2]/aside/div/div[2]/div/div[2]/div/button"))).Click();
I have a little problem clicking on submenu, the reason is that the menu tag in one frame and the submenu in other so when i switch to other frame the submenu is invisible
My code:
driver.SwitchTo().DefaultContent().SwitchTo().Frame("top");
Actions actions = new Actions(driver);
IWebElement menuHoverLink = driver.FindElement(By.PartialLinkText("Cons"));
actions.MoveToElement(menuHoverLink);
actions.Build().Perform();
driver.SwitchTo().DefaultContent().SwitchTo().Frame("content").FindElement(By.Id("elem3")).Click();
Exception
Unexpected error. Element is not currently visible and so may not be
interacted with
does anyone have an idea what can i do in that case?
Thanks.
Try to use explicit wait
driver.SwitchTo().Frame("content");
WebDriverWaitwait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("elem3"))).Click();
Or to use send keys
actions.MoveToElement(menuHoverLink).Build().Perform();
menuHoverLink.SendKeys(OpenQA.Selenium.Keys.ArrowDown);
menuHoverLink.SendKeys(OpenQA.Selenium.Keys.Enter);
I found solution but it's not always will work
i just redirect to the url that comes after clicking the option.
string urlAfterClick = "..."
driver.Navigate().GoToUrl(urlAfterClick);
Hello I am working in Selenium Automation testing with Nunit. I have one grid which have bulk of users and there is one remove button on each row of the grid. But that Remove button is visible only on mouse hover. So when I run the script, It gives me the error -
Element is not currently visible and so may not be interacted with
The Xpath of button is
"//div[1]/div[2]/div/section/div[2]/div[contains(.,'IE8 john
smith')]/div/div[2]/button[1]"
I tried working with the actions in selenium but still it gives me the same error.
Actions actions = new Actions(Driver);
var element = Driver.FindElement(By.XPath("//div[1]/div[2]/div/section/div[2]/div[contains(.,'" + fullName + "')]/div/div[2]/button[1]"));
actions.MoveToElement(element);
actions.Click();
actions.Perform();
Can anyone help me out ?
I think the following procedure will help
using Actions, hover over the element step
insert an implicit wait till the element is visible
click the button
Try to force element to be visible using JS:
IWebElement element = driver.FindElement();
js.ExecuteScript("arguments[0].style.visibility = 'visible', arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1", element);
element.Click();
I am not able to Click on SubMenu item using selenium webdriver using c#.
I am using IE9 and FireFox 13.
I have tried Action Builder but it does not work.
It gives an error saying element cannot be clicked.
WebDriverWait Wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
IWebElement menu = Wait.Until((d) => webDriver.FindElement(By.Id("id1")));
IWebElement menuOption = Wait.Until((d)=>webDriver.FindElement(By.Id("ID2")));
Actions builder = new Actions(webDriver);
builder.MoveToElement(menu).Build().Perform();
Thread.Sleep(5);
//then click when menu option is visible
menuOption.Click();
I have used even javascript :
js.ExecuteScript("return $(\"a:contains('ID1')\").mouseover();"); // Mouse hove to main menu
webDriver.FindElement(By.Id("ID2")).Click();
Please give some solution to click on hidden elements
You could use Expected Conditions to wait for element being clickable after hovering above it (Thread.sleep() is almost always the bad choice. And 5 ms won't be enough.).
The docs for this class (ExpectedConditions in OpenQA.Selenium.Support.UI namespace) are broken as I can see them now, but if you can follow the Java code in the link above, here are the Expected conditions for Java - it's really almost the same in C#, too.
Instead of using the statement Thread.sleep(). You can try to click on the element after making sure that it is displayed.
After you get the WebElement you want to click on , check if it is displayed by using the isDisplayed() method within the ExpectedContition statement about which #Slanec is talking about in the above post.
By this you can make sure you will click on the element only after Wait.Until() returns true. i.e the menuOption is displayed.
I'm writing the code in java as I do not know C#. But I guess you can figure out what I'm trying to say -
new WebDriverWait(driver, 60).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver ) {
return driver.findElement(By.Id("ID2")).isDisplayed();
}
});
I hope that this helps you.