WebDriver Methods and properties

WebDriver is an interface which has various methods to handle webpage automation.

Below are some methods which are supported in webdriver.


  • To launch the webpage
  • To find title of a webpage
  • To get current url
  • To get the page source
  • Navigate method which is useful to open a new tab,Back,Forward,Refresh of webpage.
  • find_element() and find_elements() methods which use locators such as Id, classname, CSS, name, tagName, xPath, linkText, partialLinkText to act on webelements.
  • To handle window popup
  • Switching between frames
  • Close and quite methods

get() method :

As we saw earlier this method is used to launch the url of a webpage.It takes a string as an argument and it doesn’t return anything .

Syntax : driver.get(url) (Where url is website link)
    
Example : driver.get(“http://www.skill2lead.com/”)

title property:

This property is used to get the title of the respective webpage.It doesn’t take any argument and it returns output as a string value which should be stored in a variable.

    Syntax : ele = driver.title
    print(ele)


Page_source property :

This property is used to get the source code of a respective webpage.It doesn’t take any argument and it returns output as a string value which should be stored in a variable.

    Syntax: ele =driver.page_source
    print(ele)

Current_url property:

This property is used to get the current page url which is opened in the browser. It doesn’t take any argument and it returns output as a string value which should be stored in a variable.

        Syntax: web_url = driver.current_url
       print(web_url)

Maximize_window() method :

This method is used to maximize the webpage.It doesn't take any argument and it doesn’t return anything.

        Syntax: driver.maximize_window()
        

Minimize_window() method :

This method is used to minimize the webpage.It doesn't take any argument and it doesn’t return anything.

        Syntax: driver.minimize_window()
    
        

quit() method :

This method closes all the browser windows which are opened in session.It doesn’t take any argument and it doesn’t return anything.

        Syntax: driver.quit()
    

close() method :

This method is used to close only the current browser window (webpage) which is opened in session.It doesn’t take any argument and it doesn’t return anything.

        Syntax: driver.close()
        

Example: In the below example we will launch the webpage and execute all the above methods and properties.

 


Methods_And_Properties.py

from selenium import webdriver
import time

driver = webdriver.Chrome("/Users/admin/Documents/Skill2Lead/Others/drivers/chromedriver.exe")

driver.get("http://www.dummypoint.com/seleniumtemplate.html")
time.sleep(2)

webPage_Title = driver.title
print("WebPage Title : ",webPage_Title)

current_Url = driver.current_url
print("Current URL of WebPage : ",current_Url)

page_Source = driver.page_source
print("Current Page Source : ",page_Source)

driver.maximize_window()

driver.maximize_window()

driver.maximize_window()

time.sleep(5)
driver.quit()


Let's discuss the difference between quit() and close() methods after window handling topic.