I tell Selenium to wait until it sees an element - Selenium sees it
I tell Selenium to click on this element, it is a button to link to a new page - Selenium click on it.
The problem is that after clicking it, Selenium will then wait until the next page is fully loaded (the page sometimes loads in a second, or waits for ages, I think it's a problem with Zen Desk Live Chat on that page).
When it is fully loaded it will then throw an error and say that the element it clicked on cannot be seen (naturally it can't because it is now on a new page)
I have tried changing the wait time with
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
however this doesn't help.
I have also tried
wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath(element)));
However this has the same problem.
Below is the code I am currently using.
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath(element)));
WebElement we = driver.findElement(By.xpath(element));
we.click();
System.out.println("Clicked: " + element);
}catch (Exception e){
System.out.println(e.getMessage());
}
I expect that once the element has been clicked on, that Selenium just carries on without caring if the next page has loaded up or not.
However what happens is when the 2nd page loads up, sometimes the page gets stuck "waiting for widget-mediator.zopim.com" and Selenium will not progress past the click() line until the the WebDriverWait time has expired (60 seconds)
After the 60 seconds has expired I see this error in the Console Output:
[1561374309.111][SEVERE]: Timed out receiving message from renderer: 60.000
[1561374309.112][SEVERE]: Timed out receiving message from renderer: -0.002
Is something else happening here? Or does Click() wait until the page has loaded, if that click takes it to a new page? If it does is there a way to tell it to stop waiting? I have other code to check if the page has loaded or not, but I don't need Click() to do this.
Thanks in advance.
Selenium’s (or more correctly, WebDriver’s) behavior on click is governed by the W3C WebDriver Specification. In that document, the full algorithm is defined for what happens when an element click is requested. In general, if the click will navigate to a new page, the driver will wait for that new page to be “loaded” (scare quotes intentional) according to the page load strategy until the page load timeout.
The page load strategy defaults to “normal”, or waiting for the document’s readyState to be complete. If you set it to “none” in the capabilities requested during driver instantiation, the driver will not wait at all. Choosing that route would mean you would need to handle all synchronization for pages being loaded. Note there is a third page load strategy, “eager”, but at the time of this writing, not all WebDriver implementations (most notably chromedriver) support it.
You can adjust the page load timeout at runtime in your Selenium code. One approach might be to lower the timeout to a relatively low value for the duration of clicking on this particular element, then restoring it to its prior value afterward. The drawback here is that you will have to catch the timeout exception that is thrown when the page load times out before continuing.
I am trying to use the PhantomJSDriver. The code below works with
FirefoxDriver but will not work with PhantomJSDriver. The error is:
[ERROR - 2016-02-12T16:02:47.717Z] WebElementLocator -
_handleLocateCommand - Element(s) NOT Found: GAVE UP. Search Stop Time: 1455292967683 org.openqa.selenium.NoSuchElementException: Error
Message => 'Unable to find element with id 'email''
Is there any clear guides on how to do this in Java, or can anyone get this working to login? I'm struggling to find some clarity on this topic.
I'm assuming the error is something to do with the browser being headless which therefore messes up with the paths but I have seen others using similar code and it works for them.
WebDriver driver = new PhantomJSDriver();
try {
System.out.println("Logging in to Facebook...");
driver.get("https://www.facebook.com/login");
System.out.println(driver.getTitle());
driver.findElement(By.id("email")).sendKeys("USERNAME");
driver.findElement(By.id("pass")).sendKeys("PASS");
driver.findElement(By.id("loginbutton")).click();
}
catch (Exception e) {
e.printStackTrace();
}
There's hundreds of similar questions to this one, e.g. this one. It's an issue that applies more or less equally to all browsers, and is a major cause of test instability.
Basically you're asking the Driver to find id="email" almost immediately (within milliseconds) after the page has been requested, and almost certainly before it has finished loading or that web element has been created in the DOM.
The solution is to wait until the element is ready before trying to send keys to it. See these examples. E.g.
System.out.println(driver.getTitle());
WebDriverWait wait = new WebDriverWait(driver, 10); // 10 secs max wait
wait.until(ExpectedConditions.presenceOfElementLocated( By.id("email") ));
driver.findElement(By.id("email")).sendKeys("USERNAME");
Once you know the DOM is loaded, there's no need to wait for the other elements.
There are many answers(driver.close()) there for this questions but none of them are helped me.
I try to open a website using selenium web driver for every minute.If the website was not opened within a minute code need to close the driver, but it is not closing.Because,the web page title is displaying as connecting.Please help me to work on this.
There is not a direct way to accomplish this unless you kill the browser and quit the driver instance. The easiest way probably is to add some hardcoded delay and killed the session with JavaScript
driver.get("something");
Thread.sleep(1000);
//Alternative to javaScript is to use Actions Class and send ESC key to stop execution
//Actions actions = new Actions(driver);
//actions.sendKeys(Keys.ESCAPE);
((JavascriptExecutor) driver).executeScript("window.stop;");
driver.quit();
Edit
If you want to add another way to bypass the killing of the session if in case the website loads properly try this
driver.get("");
try {
(new WebDriverWait(driver, 1)).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("something we know should exist if page load");
} catch (Exception ex) {
//Alternative to javaScript is to use Actions Class and send ESC key to stop execution
//Actions actions = new Actions(driver);
//actions.sendKeys(Keys.ESCAPE);
((JavascriptExecutor) driver).executeScript("window.stop;");
driver.quit();
}
When I am using a proxy in webdriver like FirefoxDriver, if the proxy is bad then the get method will block forever. I set some timeout parameters, but this did not work out.
This is my code:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override", ua);
Proxy p = new Proxy();
p.setHttpProxy(proxy);
profile.setProxyPreferences(p);
profile.setEnableNativeEvents(true);
// create a driver
WebDriver driver = new FirefoxDriver(profile);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
driver.get("www.sina.com.cn")
The call to driver.get will block for ever, but I want it to wait for 30 seconds and if the page is not loaded then throw an exception.
Try this:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
The timeouts() methods are not implemented in some drivers and are very unreliable in general.
I use a separate thread for the timeouts (passing the url to access as the thread name):
Thread t = new Thread(new Runnable() {
public void run() {
driver.get(Thread.currentThread().getName());
}
}, url);
t.start();
try {
t.join(YOUR_TIMEOUT_HERE_IN_MS);
} catch (InterruptedException e) { // ignore
}
if (t.isAlive()) { // Thread still alive, we need to abort
logger.warning("Timeout on loading page " + url);
t.interrupt();
}
This seems to work most of the time, however it might happen that the driver is really stuck and any subsequent call to driver will be blocked (I experience that with Chrome driver on Windows). Even something as innocuous as a driver.findElements() call could end up being blocked. Unfortunately I have no solutions for blocked drivers.
try
driver.executeScript("window.location.href='http://www.sina.com.cn'")
If you have set pageLoadStrategy to none, this statement will return immediately.
And after that , you can add a WebDriverWait with timeout to check if the page title or any element is ok.
Hope this will help you.
I had the same problem and thanks to this forum and some other found the answer.
Initially I also thought of separate thread but it complicates the code a bit. So I tried to find an answer that aligns with my principle "elegance and simplicity".
Please have a look at such forum:
https://sqa.stackexchange.com/questions/2606/what-is-seleniums-default-timeout-for-page-loading
#
SOLUTION:
In the code, before the line with 'get' method you can use for example:
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
#
One thing is that it throws timeoutException so you have to encapsulate it in the try catch block or wrap in some method.
I haven't found the getter for the pageLoadTimeout so I don't know what is the default value, but probably very high since my script was frozen for many hours and nothing moved forward.
#
NOTICE:
'pageLoadTimeout' is NOT implemented for Chrome driver and thus causes exception. I saw by users comments that there are plans to make it.
You can set the timeout on the HTTP Client like this
int connectionTimeout=5000;
int socketTimeout=15000;
ApacheHttpClient.Factory clientFactory = new ApacheHttpClient.Factory(new HttpClientFactory(connectionTimeout, socketTimeout));
HttpCommandExecutor executor =
new HttpCommandExecutor(new HashMap<String, CommandInfo>(), new URL(seleniumServerUrl), clientFactory);
RemoteWebDriver driver = new RemoteWebDriver(executor, capabilities);
The solution of driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS) will work on the pages with synch loading. This does not solve, however, the problem on pages loading stuff in async, then the tests will fail all the time if we set the pageLoadTimeOut.
I find that the timeout calls are not reliable enough in real life, particularly for internet explorer , however the following solutions may be of help:
You can timeout the complete test by using #Test(timeout=10000) in the junit test that you are running the selenium process from. This will free up the main thread for executing the other tests, instead of blocking up the whole show. However even this does not work at times.
Anyway by timing out you do not intend to salvage the test case, because timing out even a single operation might leave the entire test sequence in inconsistent state. You might just want to proceed with the other testcases without blocking (or perhaps retry the same test again). In such a case a fool-proof method would be to write a poller that polls processes of Webdriver (eg. IEDriverServer.exe, Phantomjs.exe) running for more than say 10 min and kill them. An example could be found at Automatically identify (and kill) processes with long processing time
Used below code in similar situation
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
and embedded driver.get code in a try catch, which solved the issue of loading pages which were taking more than 1 minute.
What is the difference between get() and navigate() methods?
Does any of this or maybe another method waits for page content to load?
What do I really need is something like Selenium 1.0's WaitForPageToLoad but for using via webdriver.
Any suggestions?
Navigating
The first thing you’ll want to do with WebDriver is navigate to a page. The normal way to do this is by calling get:
driver.get("http://www.google.com");
WebDriver will wait until the page has fully loaded (that is, the onload event has fired) before returning control to your test or script. It’s worth noting that if your page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded. If you need to ensure such pages are fully loaded then you can use waits.
Navigation: History and Location
Earlier, we covered navigating to a page using the get command (driver.get("http://www.example.com")) As you’ve seen, WebDriver has a number of smaller, task-focused interfaces, and navigation is a useful task. Because loading a page is such a fundamental requirement, the method to do this lives on the main WebDriver interface, but it’s simply a synonym to:
driver.navigate().to("http://www.example.com");
To reiterate: navigate().to() and get() do exactly the same thing. One's just a lot easier to type than the other!
The navigate interface also exposes the ability to move backwards and forwards in your browser’s history:
driver.navigate().forward();
driver.navigate().back();
(Emphasis added)
They both seems to navigate to the given webpage and quoting #matt answer:
navigate().to() and get() do exactly the same thing.
Single-Page Applications are an exception to this.
The difference between these two methods comes not from their behavior, but from the behavior in the way the application works and how browser deal with it.
navigate().to() navigates to the page by changing the URL like doing forward/backward navigation.
Whereas, get() refreshes the page to changing the URL.
So, in cases where application domain changes, both the method behaves similarly. That is, page is refreshed in both the cases. But, in single-page applications, while navigate().to() do not refreshes the page, get() do.
Moreover, this is the reason browser history is getting lost when get() is used due to application being refreshed.
Originally answered: https://stackoverflow.com/a/33868976/3619412
driver.get() : It's used to go to the particular website , But it doesn't maintain the browser History and cookies so , we can't use forward and backward button , if we click on that , page will not get schedule
driver.navigate() : it's used to go to the particular website , but it maintains the browser history and cookies, so we can use forward and backward button to navigate between the pages during the coding of Testcase
Not sure it applies here also but in the case of protractor when using navigate().to(...) the history is being kept but when using get() it is lost.
One of my test was failing because I was using get() 2 times in a row and then doing a navigate().back(). Because the history was lost, when going back it went to the about page and an error was thrown:
Error: Error while waiting for Protractor to sync with the page: {}
driver.get() is used to navigate particular URL(website) and wait till page load.
driver.navigate() is used to navigate to particular URL and does not wait to page load. It maintains browser history or cookies to navigate back or forward.
As per the javadoc for get(), it is the synonym for Navigate.to()
View javadoc screenshot below:
Javadoc for get() says it all -
Load a new web page in the current browser window. This is done using
an HTTP GET operation, and the method will block until the load is
complete. This will follow redirects issued either by the server or as
a meta-redirect from within the returned HTML. Should a meta-redirect
"rest" for any duration of time, it is best to wait until this timeout
is over, since should the underlying page change whilst your test is
executing the results of future calls against this interface will be
against the freshly loaded page. Synonym for
org.openqa.selenium.WebDriver.Navigation.to(String).
navigate().to() and get() will work same when you use for the first time. When you use it more than once then using navigate().to() you can come to the previous page at any time whereas you can do the same using get().
Conclusion: navigate().to() holds the entire history of the current window and get() just reload the page and hold any history.
For what it's worth, from my IE9 testing, it looks like there's a difference for URLs that contain a hashbang (a single page app, in my case):
http://www.example.com#page
The driver.get("http://www.example.com#anotherpage") method is handled by the browser as a fragment identifier and JavaScript variables are retained from the previous URL.
While, the navigate().to("http://www.example.com#anotherpage") method is handled by the browser as a address/location/URL bar input and JavaScript variables are not retained from the previous URL.
There are some differences between webdriver.get() and webdriver.navigate() method.
get()
As per the API Docs get() method in the WebDriver interface extends the SearchContext and is defined as:
/**
* Load a new web page in the current browser window. This is done using an HTTP POST operation,
* and the method will block until the load is complete.
* This will follow redirects issued either by the server or as a meta-redirect from within the
* returned HTML.
* Synonym for {#link org.openqa.selenium.WebDriver.Navigation#to(String)}.
*/
void get(String url);
Usage:
driver.get("https://www.google.com/");
navigate()
On the other hand, navigate() is the abstraction which allows the WebDriver instance i.e. the driver to access the browser's history as well as to navigate to a given URL. The methods along with the usage are as follows:
to(java.lang.String url): Load a new web page in the current browser window.
driver.navigate().to("https://www.google.com/");
to(java.net.URL url): Overloaded version of to(String) that makes it easy to pass in a URL.
refresh(): Refresh the current page.
driver.navigate().refresh();
back(): Move back a single "item" in the browser's history.
driver.navigate().back();
forward(): Move a single "item" forward in the browser's history.
driver.navigate().forward();
driver.get("url") and driver.navigate( ).to("url") both are same/synonymous.
to("url") internally calling get("url") method. Please find the below image for reference.
Either of them does not store history - this is the wrong information that is available on most of the blogs/websites.
Below, statements 1, 2, and 3, 4 will do the same things i.e land in the given URL.
statemnt 1: driver.get("http://www.google.com");
statemnt 2: driver.navigate( ).to("http://www.amazon.in");
statemnt 3: driver.get("http://www.google.com");
statemnt 4: driver.get("http://www.amazon.in");
Only navigate() can do different things i.e. moving back, forward, etc. But not the to("url") method.
Otherwise you prob want the get method:
Load a new web page in the current browser window. This is done using an
HTTP GET operation, and the method will block until the load is complete.
Navigate allows you to work with browser history as far as i understand it.
Both perform the same function but driver.get(); seems more popular.
driver.navigate().to(); is best used when you are already in the middle of a script and you want to redirect from current URL to a new one. For the sake of differentiating your codes, you can use driver.get();to launch the first URL after opening a browser instance, albeit both will work either way.
CASE-1
In the below code I navigated to 3 different URLs and when the execution comes to navigate command, it navigated back to facebook home page.
public class FirefoxInvoke {
#Test
public static void browserInvoke()
{
System.setProperty("webdriver.gecko.driver", "gecko-driver-path");
WebDriver driver=new FirefoxDriver();
System.out.println("Before"+driver.getTitle());
driver.get("http://www.google.com");
driver.get("http://www.facebook.com");
driver.get("http://www.india.com");
driver.navigate().back();
driver.quit();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
browserInvoke();
}
}
CASE-2:
In below code, I have used navigate() instead of get(), but both the snippets(Case-1 and Case-2) are working exactly the same, just the case-2 execution time is less than of case-1
public class FirefoxInvoke {
#Test
public static void browserInvoke()
{
System.setProperty("webdriver.gecko.driver", "gecko-driver-path");
WebDriver driver=new FirefoxDriver();
System.out.println("Before"+driver.getTitle());
driver.navigate().to("http://www.google.com");
driver.navigate().to("http://www.facebook.com");
driver.navigate().to("http://www.india.com");
driver.navigate().back();
driver.quit();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
browserInvoke();
}
}
So the main difference between get() and navigate() is, both are
performing the same task but with the use of navigate() you can move
back() or forward() in your session's history.
navigate() is faster than get() because navigate() does not wait for
the page to load fully or completely.
driver.get(url) and navigate.to(url) both are used to go to particular web page. The key difference is that
driver.get(url): It does not maintain the browser history and cookies and wait till page fully loaded.
driver.navigate.to(url):It is also used to go to particular web page.it maintain browser history and cookies and does not wait till page fully loaded and have navigation between the pages back, forward and refresh.
To get a better understanding on it, one must see the architecture of Selenium WebDriver.
Just visit https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol
and search for "Navigate to a new URL." text. You will see both methods GET and POST.
Hence the conclusion given below:
driver.get() method internally sends Get request to Selenium Server Standalone. Whereas driver.navigate() method sends Post request to Selenium Server Standalone.