selenium click functions working randomly - selenium

I have selenium click functions implemented by .click(), .by Actions, and by JS. None of them works stable, in other words, they work sometimes, and do not some other times. I also used Sleep functions. It is really painful. I am using selenium-server-standalone-2.52.0.jar
Can anyone give me an idea on how to solve the problems. Thanks a lot.
Charlie

Your element is probably not loaded properly while getting error. You'd better use explicit wait in your script. Wait until your specific element loaded to be visible/clickable. I think, this may help you.
Code snippet:
By yourElementToSelect = By.id("elementID"); // you may use cssSelector or any other locator you wish
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(yourElementToSelect));
driver.findElement(yourElementToSelect).click();

Related

Implicit Wait Timeout not working

Was trying to block execution for 10 seconds properly with VB.Net Selenium, so found out about Implicit Wait on SO, and found this example.
driver.Manage.Timeouts.ImplicitWait = TimeSpan.FromSeconds(10)
Debug.WriteLine(driver.PageSource)
The problem is, I set a break point on both lines and Debug.WriteLine is called nearly instantly. I've read on here I shouldn't use Thread.Sleep here, so why is the timeout not having the desired effect?
Thanks!
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
May be you missed the parentheses:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
You can also try using below:
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
Alternatively you can use ExplicitWait:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(--ELEMENT TO BE VISIBLE--));
It is more extendible in the means that you can set it up to wait for any condition you might like. Usually, you can use some of the prebuilt ExpectedConditions to wait for elements to become clickable, visible, invisible etc.
Solution is in C#.NET. You might need to convert some syntax in VB.NET.

Selenium, what is the best practice to deal with element is not clickable

I am using selenium 2.46 (firefox driver) to develop an application. There are a lot of element.click() in my code. Sometimes that elements are not visible or not clickable make the application throws selenium exception.
To resolve that issue, i use WebdriverWait(driver, 10).until(...) for each single element which needs to be clicked.
My question is there is any other better way Or design pattern that can help me to solve the problem best.
Or at least i dont have to use WebdriverWait for each single element needs to be click().
You cannot avoid WebDriverWait. If you send a webdriver click command, webdriver will blindly assume that "element is clickable". You need to instruct webdriver to wait because your element is special and needs some synchronization before it can click on it. I don't think you need to do this for every other element. You can incorporate ExpectedConditions so that you can keep your code snippets manageable and small. So something like,
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("foo"))).click();
The other option you can try other than clicking is hit enter on respective element, for that you can refer ID of that element.
driver.findElement(By.id("elementid")).sendKeys(Keys.ENTER);
use implicit wait instead of explicit wait and give the expected condition till the element doesn't visible on screen.
for more info you can check
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#invisibilityOfElementLocated-org.openqa.selenium.By-
Hope this will help you

NoSuchElementException while using Xpath with HtmlUnitDriver

I need to run Selenium in Linux machine and using HtmlUnitDriver to achieve this.
Selenium script contains most of Xpath to find the elements. While running, NoSuchElementException is getting displayed at the places where xpath is used.
org.openqa.selenium.NoSuchElementException: Unable to locate a node using //*[contains(text(),'Sample Text')]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
The same code is working fine with other drivers like Chrome, Firefox or IE.
My code looks as below,
HtmlUnitDriverdriver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get("http://example.com");
driver.findElement(By.id("username")).sendKeys("xxx");
driver.findElement(By.id("password")).sendKeys("yyy");
driver.findElement(By.id("loginButton")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[contains(text(),'Sample Text')]")).click();
I found many similar question and the solution is to provide sleep time or to wait for the element till it gets loaded. I provided everything and still facing this issue when xpath is used.
Also finding the element by ID or name is not possible in my use case as it varies everytime.
I can use only the xpath to find the text inside the span tag.
I need a solution for handling this. Thanks in advance!
Edit :
I tried the same for gmail login, Still am getting the same issue,
driver.get("http://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("xxx#gmail.com");
driver.findElement(By.id("next")).click();
Thread.sleep(2000);
driver.findElement(By.id("Passwd")).sendKeys("yyy");
driver.findElement(By.id("signIn")).click();
Thread.sleep(8000);
driver.findElement(By.xpath("//*[contains(text(),'COMPOSE')]")).click();
Consider also getting rid of text(), which might cause that kind of problem. Use this instead:
contains(., 'Sample Text')
Not sure this is the cause if your problem here, this is more of a blind shot, given the few info you gave us.
I would first of all suggest providing a more effective explicit wait, so that it will suspend until the condition is fulfilled, and also will give you the TimeoutException if you didn't provide long enough timeout.
//This will store your locator for later use
By elementLocator = By.xpath("//*[contains(text(),'Sample Text')]");
//This creates a reusable wait object
WebDriverWait wait = new WebDriverWait(browser.driver, 15);
//Waiting is stopped right after the condition is satisfied or timeout (set in the previous line, in secs) was reached
wait.until(ExpectedConditions.elementToBeClickable(elementLocator));
If you are getting the same error you can try this
try {
wait.until(ExpectedConditions.elementToBeClickable(elementLocator));
} catch (org.openqa.selenium.TimeoutException e) {
System.out.println(driver.getPageSource());
}
To see whether the element is present on the page you are looking for it.

What to do when waiting for an element is not enough?

I am writing selenium test scripts using the industry standard of webdriver waits before interacting with elements, but I still frequently find my tests are failing, and it seems to be due to a race condition.
Here's the example I have been running into lately:
Go to the product catalog page
Apply a filter
Wait for the filter to be applied
Click the save button on the product which loads after the filter is applied
Step number 4 only works if I place a Thread.Sleep() in front of the step - using webdriverwait is not enough. I'm guessing this is because the webdriverwait only waits until the element is attached to the DOM, even though the relevant JavaScript click event has not been added to the element.
How do you get around this issue? Is there an industry standard for dealing with this race condition?
EDIT This was resolved by upgrading to the latest version firefox. Thanks everyone!
As we discovered in comments, updating Firefox to the latest version did the trick.
The code looks really good to me and makes total sense.
What I would try is to move to the element before making a click:
Actions builder = new Actions(WebDriver);
IWebElement saveButton = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector(".button-wishlist")));
Actions hoverClick = builder.MoveToElement(saveButton).Click();
hoverClick.Build().Perform();
As we've discovered in comments, the issue is related to the size of the window (the test passed without a Thread.sleep() if the browser window is maximized). This makes me think that if you scroll to the element before making a click it could be enough to make it work:
IWebElement saveButton = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector(".button-wishlist")));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", saveButton);
Actions hoverClick = builder.MoveToElement(saveButton).Click();
hoverClick.Build().Perform();
Take a look at this SO post for custom wait method. Sounds like element presence is not enough of a check in your case because the button may be present at all times in the DOM. What you need is something along the lines of ExpectedConditions.elementToBeClickable().
I am not familiar with the C# API but it looks like there is no built in method to do the same thing as in Java. So you could write a custom wait function that will have checks according to your needs.

How to make xpath work with Selenium when filepicker.io is loaded

After implementing filepicker.io, some of our Selenium regression tests have started failing. The failures (intermittent, but more often than not in some circumstances) are that clicks are ignored on WebElements found via XPath queries. e.g.
driver.findElement(By.xpath("//a[text()='Demo data']")).click();
Adding a Sleep(2000) between findElement() and click() generally resolves the problem. (I say generally because Sleep(1000) was mostly enough, until it wasn't, so I made it Sleep(2000)...)
Checking element.isDisplayed() has not helped. The problem disappears if we stop including the filepicker.io JavaScript file.
Is it something to do with filepicker.io introducing an IFRAME? We have also noticed that JQuery's document.ready() seems to be now invoked twice.
As usual with this kind of problems, you are trying to find an element that is not yet available on the page due to AJAX request still downloading/processing it. You need to wait for the element to appear on the page.
There are three ways to do this:
Using sleep(). This is the discouraged way. You should not use hardcoded sleeps, because you'll either wait too long (making the tests unnecessarily slow) or too short (failing the test).
Use Implicit wait. That will always wait for an element if it's not found.
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Use explicit wait. That enables you to wait explicitly for one element to (dis)appear / become available / whatever.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Demo data")));
We now run this code first after opening any page that includes filepicker.js:
while (FindElementsMaybeNone(By.cssSelector("#filepicker_comm_iframe")).size() == 0)
Sleep(50);
while (driver.switchTo().frame("filepicker_comm_iframe") == null)
Sleep(50);
driver.switchTo().defaultContent();
We guess that filepicker's dynamic IFRAME insertion is discombobulating Firefox or Selenium. I'm not marking this as the answer because I don't really know why it works.