- Driver get selenium java
- Method Detail
- get
- getCurrentUrl
- getTitle
- findElements
- findElement
- getPageSource
- close
- quit
- getWindowHandles
- getWindowHandle
- switchTo
- navigate
- How To Load URL In Selenium WebDriver – get() & navigate()
- Opening a URL through get() method:
- Navigation Commands in Selenium
- Different ways to refresh a page
Driver get selenium java
Return a set of window handles which can be used to iterate over all open windows of this WebDriver instance by passing them to switchTo() . WebDriver.Options.window()
Method Detail
get
void get(java.lang.String url)
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 (with the default ‘page load strategy’. 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 WebDriver.Navigation.to(String) . See W3C WebDriver specification for more details.
getCurrentUrl
java.lang.String getCurrentUrl()
Get a string representing the current URL that the browser is looking at. See W3C WebDriver specification for more details.
getTitle
findElements
Find all elements within the current page using the given mechanism. This method is affected by the ‘implicit wait’ times in force at the time of execution. When implicitly waiting, this method will return as soon as there are more than 0 items in the found collection, or will return an empty list if the timeout is reached. See W3C WebDriver specification for more details.
findElement
Find the first WebElement using the given method. This method is affected by the ‘implicit wait’ times in force at the time of execution. The findElement(..) invocation will return a matching row, or try again repeatedly until the configured timeout is reached. findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead. See W3C WebDriver specification for more details.
getPageSource
java.lang.String getPageSource()
Get the source of the last loaded page. If the page has been modified after loading (for example, by Javascript) there is no guarantee that the returned text is that of the modified page. Please consult the documentation of the particular driver being used to determine whether the returned text reflects the current state of the page or the text last sent by the web server. The page source returned is a representation of the underlying DOM: do not expect it to be formatted or escaped in the same way as the response sent from the web server. Think of it as an artist’s impression. See W3C WebDriver specification for more details.
close
Close the current window, quitting the browser if it’s the last window currently open. See W3C WebDriver specification for more details.
quit
getWindowHandles
java.util.Set getWindowHandles()
Return a set of window handles which can be used to iterate over all open windows of this WebDriver instance by passing them to switchTo() . WebDriver.Options.window() See W3C WebDriver specification for more details.
getWindowHandle
java.lang.String getWindowHandle()
Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later date See W3C WebDriver specification for more details.
switchTo
navigate
How To Load URL In Selenium WebDriver – get() & navigate()
Launching a web browser and loading a URL in opened browser are basic steps to get started with Selenium WebDriver. We have already learnt to launch different browsers using Selenium WebDriver. In this post, we will see how can we load a URL in browser.
In this post we will learn below topics:
- Loading a URL through get() method.
- Loading a URL using to() method.
- Difference between get() and navigate()
Opening a URL through get() method:
get() method is used to load a web page or URL in a browser using an HTTP POST operation i.e.
POST /session/:sessionId/url
:sessionId – ID of the session to route the command to. url – The URL to navigate to passed as body.
Implementation of get() method is in RemoteWebDriver class which creates a POST request internally (execute() method) and you have no need to worry abut creating an API request explicitly.
Method signature is as below :-
void get(java.lang.String url)
Points to know about get():
- get() method is declared in WebDriver interface.
- get() method is implemented in RemoteWebDriver class which construct an HTTP POST request.
- get() method returns void.
- get() method takes an argument of type String which is a URL.
- It loads a new web page in the current browser window.
- It blocks the execution of next statement till web page is loaded completely.
- If you try to open a URL without HTTP or HTTPS i.e. driver.get(“www.facebook.com”);) then get() method will throw an exception as “org.openqa.selenium.InvalidArgumentException: invalid argument“. This is common mistake done by beginners.
- You can load a URL without mentioning WWWi.e. http://facebook.com. It will not give any exception. This was asked to me in interview.
- There may be a Page Load Timeout which depends upon browser. For chrome, page load time out is shown as 300000 seconds as shown below :-
10. We can set page load time using pageLoadTimeout( long arg0, TimeUnit arg2 ) method.
12. You can go back and forward using interface Navigation method as browser history is maintained. You can use get() multiple times in a program.
Navigation Commands in Selenium
Navigation commands enable the user to work with a history of the browser like back, forth, refresh, to. We can access these methods by accessing the navigate() method present in the driver (FF, IE, GC, Safari) class; we can access these methods we can access through navigate() method are below:
- to(String string)
- to(Url url) //overloaded to() method
- back()
- forward()
- refresh()
to(String value):
to(string) method navigates the user to the particular webpage
Elaborated code for to(String value):
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver.Navigation; import org.openqa.selenium.firefox.FirefoxDriver; public class To < public static void main(String[] args) throws Exception < WebDriver driver=new FirefoxDriver(); driver.get("https://chercher.tech"); Navigation nav = driver.navigate(); nav.to("https://gmail.com"); >>
Elaborated code for to(URL url):
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver.Navigation; import org.openqa.selenium.firefox.FirefoxDriver; public class To < public static void main(String[] args) throws Exception < WebDriver driver=new FirefoxDriver(); driver.get("https://selenium-mentor.com"); driver.navigate().to(new URL(https://gmail.com)); >>
back():
back() method helps the user to navigate back.
Optimized code for navigate back()
In the above program, you might not face any error, but the browser also will not navigate anywhere, why? Because back() calls the back() native method present in the browser indirectly, so back works only when you navigate more than one page.
Modify the above program like below one
forward():
The forward() method navigates the user to forth but if we want forward() method to work we should use back() before using forward() then only the forward button in the browser or it creates the place in the browser history Syntax:driver.navigate().forward();
refresh():
refresh() method refreshes the current webpage not suggested for https pages
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Refresh < public static void main(String[] args) throws Exception < WebDriver driver=new FirefoxDriver(); driver.get("https://chercher.tech"); driver.navigate().refresh(); >>
Different ways to refresh a page
1.Using sendKeys.Keys method
driver.get("https://chercher.tech"); driver.findElement(By.tageName("body")).sendKeys(Keys.F5);
2.Using navigate.refresh() method
driver.get("https://chercher.tech"); driver.navigate().refresh();
3.Using navigate.to() method
driver.get("https://chercher.tech"); driver.navigate().to(driver.getCurrentUrl());
4.Using get() method
driver.get("https://chercher.tech"); driver.get(driver.getCurrentUrl());
5.Using sendKeys() method using keyboard
driver.get("https://accounts.google.com/SignUp"); driver.findElement(By.id("firstname-placeholder")).sendKeys("uE035");
Recommended Readings
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.
thank you for the article which you posted with neat and clean explanation
Thanks Murali Krishna, we are glad, it was useful to you