WebDriver is an interface which has various methods to handle webpage automation.
Below are some methods which are supported in webdriver.
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/”)
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)
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)
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)
This method is used to maximize the webpage.It doesn't take any argument and it doesn’t return anything.
Syntax: driver.maximize_window()
This method is used to minimize the webpage.It doesn't take any argument and it doesn’t return anything.
Syntax: driver.minimize_window()
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()
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.