Selenium RC command selenium.waitForPageToLoad("30000") is not working in WebDriver.
Is there any alternate command for this in WebDriver?
There are two types of waits you can use in Selenium; implicit and explicit.
Below examples are written in Java:
1) Explicit Wait:
new WebDriverWait(super.getDriver(), 10).until(ExpectedConditions.elementToBeClickable(site_logo));
Above code will wait 10 seconds for site logo element to be clickable, if not it will throw an exception. ExpectedConditions class has bunch of other methods you can use. You can check whether an element is present or not etc...
2) Implicit Wait:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
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.
There is also Thread.sleep(Time in milliseconds); method, but I don't recommend you to use this one.
For more information: http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
You can use WebDriveWait to solve it:
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
For waiting to an element, use wait.until(ExpectedConditions.visibilityOfElementLocated) :
#Test
public void test1() throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 1);
driver.get("example.html");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(elementLocation)));
driver.close();
}
Related
In selenium testing framework I am using thread.sleep(40000). I have a requirement not to use thread.sleep()or its alternative fluent wait or any kind of wait, and keep your script engaged anyhow so that script will pick particular element after that some interval until that element actually appears on that page. Hence error wont be thrown while accessing the element. Do you have any suggestion how can I keep my script engaged for few miliseconds without using any wait ?
I use this code. You can add try catch with timeOutException.
import org.openqa.selenium.By.*;
By element = new ById("id");
long timeout;
WebElement webElement =(WebElement)(new WebDriverWait(getDriver(), timeout)).until(ExpectedConditions.visibilityOfElementLocated(element));
I've written a script using Selenium with Java. It is working fine sometimes without any kind of exceptions. But sometimes I'm getting TimeOutException as I've used explicit wait. Does this kind of behavior relate to the application? What could be the problem?
ChromeOptions options = new ChromeOptions();
options.addArguments("incognito");
WebDriver driver = new ChromeDriver(options);
driver.get("url");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.id("usernameid")));
driver.findElement(By.id("usernameid")).sendKeys("632145");
wait.until(ExpectedConditions.elementToBeClickable(By.id("passwordid")));
driver.findElement(By.id("passwordid")).sendKeys("1234");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//button[#type='button']")));
driver.findElement(By.xpath(".//button[#type='button']")).click();
The script is failing sometimes at the button. I'm getting TimeOutException.
Mean while just use the "Waiter" api with lots of wait combinations..
<dependency>
<groupId>com.imalittletester</groupId>
<artifactId>thewaiter</artifactId>
<version>1.0</version>
</dependency>
Here, you are using WebDriverWait for 20 seconds on each element without setting ImplicitWait. If you need to wait for 20 seconds definitely on each element, first set ImplicitWait with more than 20 seconds and then use WebDriverWait.
As a side note, ImplicitWait will be applicable only on findElement and findElements methods.
The default timeout what selenium uses to find element is 0 seconds if we are not setting up ImplicitWait.
You can refer more details on this from this url: The default value of timeouts on selenium webdriver
I am a beginner. I understand what waits basically does but I am confused over how different tutorials over the internet place it and explain it. For example, in the below code it is placed before loading the URL. So, is it only to wait for the URL to be loaded or for finding the element or both? Is is true that if I use an implicit wait once in my try block, it will be applicable for every element search I am performing in my code?
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")
ImplicitWait
ImplicitWait as per the Java Docs is to specify the amount of time the WebDriver instance i.e. the driver should wait when searching for an element if it is not immediately present in the HTML DOM in-terms of NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS or DAYS when trying to find an element or elements if they are not immediately available. The default setting is 0 which means the driver when finds an instruction to find an element or elements, the search starts and results are available on immediate basis.
In this case, after a fresh loading of a Webpage an element or elements may be / may not be found on an immediate search. So your Automation Framework may be facing any of these exceptions:
NoSuchElementException
TimeoutException
ElementNotVisibleException
ElementNotSelectableException
Hence we introduce ImplicitWait. By inducing ImplicitWait the driver will poll the DOM Tree until the element has been found for the configured amount of time looking out for the element or elements before throwing a NoSuchElementException. By that time the element or elements for which you had been looking for may be available in the HTML DOM. As in your code you have already set ImplicitWait to a value of 10 seconds, the driver will poll the HTML DOM for 10 seconds.
Python:
driver.implicitly_wait(10)
Java:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
DotNet:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
Finally, once you set the ImplicitWait, the WebDriver instance i.e. the driver is able to carry this configuration till its lifetime. But if you need to change the coarse of time for the WebDriver instance i.e. the driver to wait then you can reconfigure it as follows:
Python:
driver.implicitly_wait(5)
Java:
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
DotNet:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
If at any point of time you want to nullify the ImplicitWait you can reconfigure it as follows:
Python:
driver.implicitly_wait(0)
Java:
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
DotNet:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
Answering your questions
...Wait for the URL... : No, ImplicitWait have no effect on page loading.
...For finding the element... : Yes, ImplicitWait will define the coarse of time the WebDriver instance will wait looking out for the element or elements.
...Implicit wait once... : Yes, you need to configure ImplicitWait only once and it is applicable throughout the lifetime of the WebDriver instance.
...Every element search... : Yes, applicable when ever findElement() or findElements() is invoked.
yes, implicit_wait is globally applicable. so once you set it's applied to all the element.
I would not suggest to use implicit_wait unless your application is too slow. You could use explicit wait or any other wait based on your requirement from the following page.
it's a JAVADOC but implementation should be same for python as well.
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html#implicitlyWait-long-java.util.concurrent.TimeUnit-
Implicit wait is applicable for all the web elements where as Explicit wait is applicable only for the element it is specified.
Explicit wait is more intelligent and are really use full in handling Ajax on the other hand implicit wait is generally used to handle application sync issues.
I am new to Selenium. This is my first attempt. And I want to access the Elements in the new window through Selenium RC. I have a page with a hyper link clicking on it will open new page. I want to enter username and password elements in the new window. Html Code is
Employee Login
and the new page elements are "emailAddress" and "password" for login.
My selenium code is
public static void main(String args[]) throws Exception
{
RemoteControlConfiguration rc=new RemoteControlConfiguration();
rc.setPort(2343);
SeleniumServer se= new SeleniumServer(rc);
Selenium sel=new DefaultSelenium("localhost",2343,"*firefox","http://neo.local/");
se.start();
sel.start();
sel.open("/");
sel.windowMaximize();
//sel.wait(1000);
sel.click("empLogin");
//sel.wait(2000);
//sel.openWindow("http://myneo.neo.local/user/login", "NewNeo");
//sel.waitForPopUp("NewNeo", "1000");
//sel.selectWindow("id=NewNeo");
Thread.sleep(20000);
sel.type("emailAddress", "kiranxxxxx#xxxxxx.com");
sel.type("password", "xxxxxxxx");
}
First I tried with normal approach, where it failed to identify the elements. Then I tried with open window and selectWindow options, where it through errors like
"Exception in thread "main" com.thoughtworks.selenium.SeleniumException: ERROR: Window locator not recognized: id
at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:109)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:103)
at com.thoughtworks.selenium.DefaultSelenium.selectWindow(DefaultSelenium.java:377)
at Demo.main(Demo.java:24)"
Some one told me that it is not possible with Selenium RC. It can achieved through Selenium Webdriver only. Is it true?
Please Help,
Thanks in Advance.
Selenium RC is not maintained anymore, I would recommend you to use WebDriver instead. Selenium RC is a Javascript application where as WebDriver uses browsers Native API. Therefore browser interactions using WebDriver are close to what the real user does. Also WebDriver's API is more intuitive and easy to use in my opinion. I don't see HTML in your question, but you could do start with something like this,
WebDriver driver = new FirefoxDriver();
WebElement login = driver.findElement(By.id("empLogin"));
login.click();
I will say, do as #nilesh stated. Use Selenium WebDriver.
Answer to your specific problem though:
You are failing at this line because you are not specifying a selector strategy.
sel.click("empLogin");
If the id attribute is empLogin then do
sel.click("id=empLogin");
There are other selector strategies you can use:
css=
id=
xpath=
link=
etc...
You can see the full list here.
You will also fail here due to the same issue:
sel.type("emailAddress", "kiranxxxxx#xxxxxx.com");
sel.type("password", "xxxxxxxx");
Put a selector strategy prefix before those fields.
sel.type("name=emailAddress", "kiranxxxxx#xxxxxx.com");
sel.type("name=password", "xxxxxxxx");
I'm using the FireFox WebDriver for Selenium. I have a window that gets loaded via AJAX and my tests are inconsistent due to this.
I can put a 3 sec delay on the thread and it works everytime. All I'm doing is checking for the present of a row.
What's the best way to do this?
Presumably your Selenium tests are supposed to simulate a person using the site, so anything you do to circumvent what a human would do (sit and wait) could potentially lead to an erroneous result. In situations like this I see no problem in adding a delay while the AJAX loads (or fails) and then reporting that.
the best way (IMHO) is to use fluent wait.
Anyway you have locators of the needed element (xpaths, or css selectors or etc.) and passing locators to fluentWait function you'll obtain needed web elements to interact with.
fluentWait function code provided below:
public WebElement fluentWait(final By locator){
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(
new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
}
);
return foo; } ;
it is important to note that each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.(c)selenium API documentation
So you can set
.pollingEvery(1, TimeUnit.SECONDS)
and during this interval will be verified wheter your AJAX has returned or not.
some add info about fluent wait you can read here
Assume
String cssLocator=...blablabla.. // is the css of the table row
you pass to the function:
WebElement firstRow= fluentWait(By.cssSelector(cssLocator));
and in this way you obtain first row of your table.
Hope this got clear to you now)