Important Python Selenium Functions You Must Know

Must Know Functions In Selenium Python

In this tutorial, we’ll explore the most useful methods of selenium. Selenium provides lots of methods that make the process of automation easy, some of which are not commonly known but yet are useful to make the process of automation/scraping easy and effective.

Also read: Installing and Setting up Selenium

Importing Modules And Initializing Webdriver

Selenium webdriver is the parent class of all the classes and methods. Using the webdriver we access all the methods selenium provides.

from selenium import webdriver

driver = webdriver.Chrome("path of webdriver")

In place of “path of webdriver”, we can even just use the Chrome() method, without passing any location parameter of webdriver’s path, provided we have declared the path location of our browser’s webdriver as an Environment variable globally in our computer.

Browser Methods in Python Selenium

Let’s begin with the browser methods that you’ll use most often and should have a good hands-on with.

1. Fetching a webpage

To fetch a specific webpage we use the get method and pass the webpage URL as the parameter.

driver.get("https://m.youtube.com")

2. Fetch title of a Webpage

title method returns the title of the current page (webpage), the title is printed in the console/terminal of the code execution.

driver.title()

3. Moving back and forth between page history

By using back and forward methods, we can automate the task of going back or forward in a webpage in a browser

driver.back()
driver.forward()

4. Fullscreen method

Calling this method will make the browser (chrome) window Fullscreen.

driver.fullscreen_window()

5. Set Window Position on screen

By using this method, you will be able to set coordinates of the current windows. This method takes x and y coordinates and sets the current window(opened using python-selenium code) at that specified position as per the coordinates on the screen.

driver.set_window_position(500,500)

The origin (0,0) of the screen position is the bottom-most left part of the screen.

6. Open a New Tab

To open a new website on a new tab, we’ll use the execute_script() method. In our example below, the code will open Twitter in a new tab alongside our first website, YouTube. This method takes JavaScript to execute.

driver.execute_script("window.open('https://twitter.com')")

7. Taking Screenshot

Selenium provides a method for taking a screenshot of the current window. It is done by using the following method

driver.get_screenshot_as_file('ss.png')

An image of the name ‘ss’ will be stored in the same directory after running this code.

8. Refresh Page

By using the refresh method, we can refresh the current frame/webpage.

driver.refresh()

9. Select an Element

There are various methods and techniques to select an element, image, text field, video, tag, etc. in a webpage. Hence, we have covered all the methods to select an element in a separate, detailed article.

Recommended Read: All different ways to Select an Element in Selenium – Python

10. Click on an Element

This method is used to click on web elements like a button or a link.

In the code below we first find the link or button using its ID or any other selector, then we call the click method on this web element.

elements = driver.find_element_by_id("id of link or button")
elements.click()

11. Send Keys (text)

We can send some text to any text field in a webpage by using this method. We find the text box for Gmail by using its id and then send our text with the send_keys method.

elements = driver.find_element_by_id("gmail")
elements.send_keys(“some text we want to send”)

12. Clear the text

It is used to clear the text of any input field.

elements = driver.find_element_by_id("gmail")
elements.send_keys("some text we want to send")
elements.clear()

13. Use custom JavaScript

Using this method we can send in custom JavaScript code and perform a variety of operations as supported by JavaScript for events, prompts, etc.

driver.execute_script()

#practical code implementation
driver.execute_script("window.open('https://google.com')")

14. Close the current tab without closing the browser

By using the close method we can close the current tab without closing the browser.

driver.close()

15. Close the browser

We can close the browser with the quit method. Obviously, the whole browser window closes, closing all the tabs opened.

driver.quit()

16. Time – Sleep (Imp.)

This is actually not functionality or method associated with the Selenium library but it’s a very useful tip, which comes in handy for various purposes like waiting for some task to perform – loading of the site, execution of some other code, etc.

This is part of the time module, which already comes bundled with our python installation:

import time

#time.sleep(time to wait (in seconds))

time.sleep(5) #this will let the interpreter wait at this line for 5 seconds and then proceed with execution.

Conclusion

That’s It for the tutorial. There are various methods and functionalities associated with Selenium, some of which you’ll yourself learn while using Selenium and some which we insist to look around at the official documentation of Selenium.