Basic Browser Commands

Now that we have been successful in creating and running our first script in both Firefox and Chrome, it’s time we start looking at some of the commands and functions that Selenium provides us with.

Since Selenium is designed to work with web based applications, we need to be able to open and pass parameters to out web applications using Selenium.

Let us see how and which methods are provided by Selenium which allows us to play with basic browser functions.

 

  • get( )

The get( )method is one of the most important methods of the whole Selenium API. This is the method which will allow you to open a URL.

The parameter that is passed to this method is the valid URL, which you would want to be navigated.

 

  • title

The purpose of this attribute is to get the page title of a web page.

 

  • current_url

The purpose of this attribute is to get the current page URL. This gets the current url loaded in the browser.

 

  • page_source

This attribute is used to get the current page source.

 

  • close( )

This method will close the current window. If there are multiple windows open, this method will close the most current window.

 

  • quit( )

This is almost as same as close( ) method. Except, that it will close all the current instances of the browser.

 

  • refresh( )

As the name suggests, refresh( ) method is used to refresh the current browser instance.

 

Here in this script, we would go to the ToolsQA website and apply these basic browser commands. We’re gonna get the title, the page source, along with trying to refresh the browser.

import unittest
from selenium import webdriver

class logintest(unittest.TestCase):
    def setUp(self):
       self.driver=webdriver.Firefox()
    def test_filldetails(self):
       driver=self.driver
       driver.maximize_window()
       driver.get('http://www.toolsqa.com/')

       driver.implicitly_wait(30)

       title1=driver.title
       print(title1)

       print(len(title1))

       url1=driver.current_url

       print(url1)
       print(len(url1))

       driver.refresh()

       print(driver.page_source)
       print(len(driver.page_source))

  def tearDown(self):
      self.driver.quit()
if __name__ == '__main__':
unittest.main()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.