Why should I use driver.find instead of wait.until in Selenium? - selenium

In Selenium, I can find an element by using driver.find, and by using the explicit wait wait.until. Explicit wait waits until the element is clickable, then it proceeds to the next step. Why should I use driver.find and risk timing out in case the element is for some reason not found, when I can just put wait.until everywhere? Is there a performance penalty or am I missing something?

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.

Browser runs faster than webdriver selenium command

I am performing an operation using selenium webdriver to wait for an element until an element is visible. After a few milliseconds, it gets disappeared(Expected).Generally we use explicit wait to synchronize with browser because browser is slower. But in this case, browser is faster and before command waits for the visibility , the element disappears hence failing the operation.
It would be great if anyone can help regarding the issue.
PS I am using jmeter webdriver plugin.
Thanks.
You could handle exception which breaks your validation (ignore NoSuchelementException but fail validation on TimeoutException) or create waiting method which waits for element to appear and after that wait to disappear.

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

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.

Selenium WebDriver. After waiting for the element click() freezes test

I need to click on the dynamically generated element using Selenium WebDriver (Java API).
I'm waiting for this element to appear with WebDriverWait and then clicking on it. This click succeeds but the following click on the different static element freezes the whole test. Here is the code:
webDriver.get(alfrescoURL + "/share/page/create-document");
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement documentTypeList = webDriver.findElement(By.id("template_x002e_create-document_x002e_create-document_x0023_default_documenttype-selected-form-button-button"));
documentTypeList.click();
WebElement listItem = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("yui-gen100")));
listItem.click();
// Choosing to create in new project
WebElement projectLink = webDriver.findElement(By.id("template_x002e_create-document_x002e_create-document_x0023_default_projecttype-entry1"));
projectLink.click();
documentTypeList.click() opens a drop-down list, listItem.click() chooses an item, projectLink.click() makes a choice in the group of radiobuttons. Test silently freezes on projectLink.click(). It looks like this click() infinitly waits for page reloading that happens by some reason while it shouldn't. (Disappearing of the list after choosing an item is made by javascript that doesn't make any AJAX requests.)
I think there is something about click() blocking i don't understand. It says in it's javadoc that it attempts to block only if it causes a page to load. Nevertheless here i get a block for some reason.
If i insert a thread sleep before projectLink.click() then test works fine. It agrees with a hypothesis that i get a infinite block on click().
Thanks in advance.
I've run into this before where the test runs faster than the drop down can contract and can't click the following element. Instead of using arbitrary sleeps (although in rare cases they are necessary), can you put in a wait for a class change in the drop down?
For example, if I want to wait for the drop down to contract before moving on, I'll wait for the class of the select to change from "active" to "closed". This, of course, assumes your HTML has these dynamic classes in place.
Another possibility is to set an implicit wait, giving yourself enough padding for instances like these:
driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);
I would suggest that you try other click options too:
a) Actions#click()
b) Javascript click()
If any of those click works then it means the issue is with the selenium's WebElement Click method which needs to be reported.