Tuesday, September 10, 2019

Post#11.Open Webpage


Open Webpage

We can open the browser by using get(“᾿) a non-static method present in the FirefoxDriver class, it accepts a string as arguments and the string should be the website address.

A special thing about this get(“᾿) method is it will not give control to the next line till the page loads completely.

package pack2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenWebSite
{
            public static void main(String[] args) throws Exception
            {
                        System.setProperty("webdriver.gecko.driver", "D:\\BrowserDrivers\\geckodriver.exe");
                        WebDriver driver=new FirefoxDriver();

                        // tries to open google page
                        driver.get("www.google.com");
            }
}

Not Able to Open Website with above code?

Did you get following error?

Exception in thread "main" org.openqa.selenium.InvalidArgumentException: Malformed URL: www.google.com is not a valid URL.

Solution:
Whenever we pass the website address to the get() method .it first of checks for the protocol in the address ,but in the above program we did not mention any the protocol. 


What is protocol?

Protocol is simple it mention what kind of site is that.
E.g. for protocol:
-http
-https
-ftp
-file


Let's edit the above program

package pack2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenWebSite
{
            public static void main(String[] args) throws Exception
            {
                        System.setProperty("webdriver.gecko.driver", "D:\\BrowserDrivers\\geckodriver.exe");
                        WebDriver driver=new FirefoxDriver();

                        // tries to open google page
                        driver.get("https://www.google.com");
            }
}

Hope this time you are able to launch google site.
Note: If the site doesn't have the protocol then just copy paste the url into eclipse it will automatically imports the protocol.


No comments:

Post a Comment