The Wayback Machine - https://web.archive.org/web/20240619070445/https://www.geeksforgeeks.org/interacting-with-webpage-selenium-python/
Open In App

Interacting with Webpage – Selenium Python

Last Updated : 18 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provide a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout – Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful. What we’d really like to do is to interact with the pages, or, more specifically, the HTML elements within a page. First of all, we need to find one. WebDriver offers a number of ways to find elements. For example, given an element defined as:  

html




<input type="text" name="passwd" id="passwd-id" />


To find an element one needs to use one of the locating strategies, For example, 

element = driver.find_element(By.ID,"passwd-id")
element = driver.find_element(By.NAME, "passwd")
element = driver.find_element(By.XPATH, "//input[@id='passwd-id']")

Also, to find multiple elements, we can use 

elements = driver.find_elements(By.NAME, "passwd")

One can also look for a link by its text, but be careful! The text must be an exact match! One should also be careful when using XPATH in WebDriver. If there’s more than one element that matches the query, then only the first will be returned. If nothing can be found, a NoSuchElementException will be raised. 

WebDriver has an “Object-based” API, we represent all types of elements using the same interface. This means that although one may see a lot of possible methods one could invoke when one hits IDE’s auto-complete key combination, not all of them will make sense or be valid. To check all methods, checkout Locator Strategies – Selenium Python 

So after getting an element what next? One might want to enter text into a field, for example, 

element.send_keys("some text")

One can simulate pressing the arrow keys by using the “Keys” class:  

element.send_keys(" and some", Keys.ARROW_DOWN)

Also note, that it is possible to call send_keys on any element, which makes it possible to test keyboard shortcuts such as those used on Gmail. One can easily clear the contents of a text field or textarea with the clear method:

element.clear()

Project Example: Let us try to search for something automatically at geeksforgeeks 

Program

Python3




# import webdriver
from selenium import webdriver
from selenium.webdriver.common.by import By
 
# create webdriver object
driver = webdriver.Firefox()
 
 
# get geeksforgeeks.org
 
# get element
element = driver.find_element(By.ID, "gsc-i-id1")
 
# send keys
element.send_keys("Arrays")


Output: 

Interacting-with-Webpage-Selenium-Python



Previous Article
Next Article

Similar Reads

How to get title of a webpage using Selenium in Python?
The title method is used to retrieve the title of the webpage the user is currently working on. It gives the title of the current webpage loaded by the driver in selenium, if webpage has no title a null string will be returned. Webpage title :A page title, also known as a title tag, is a short description of a webpage and appears at the top of a br
2 min read
Count total number of Links In Webpage Using Selenium In Python
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. Installation 1.1 Selenium Bindings in PythonSelenium Python bindings provide
2 min read
How to click a button on webpage using Selenium?
This article is all about how to click any button using Selenium on a webpage and many more concepts related to the same which are discussed below. Table of Content What is Selenium?How to click on a button using SeleniumConclusionFrequently Asked Questions on How to click a button on webpage using selenium?What is Selenium?Selenium is a widely use
2 min read
How to Clone webpage Using pywebcopy in Python?
Sometimes we need a handy web page on your local hard drive. So, here we are going to write a simple Python script to Scrap a web page. Web scraping is used for extracting data from websites for offline reading, storage, or whatever reason. Before writing the script we need to know pywebcopy. pywebcopy is available on PyPi and is easily installable
1 min read
Extract all the URLs from the webpage Using Python
Scraping is a very essential skill for everyone to get data from any website. In this article, we are going to write Python scripts to extract all the URLs from the website or you can save it as a CSV file. Module Needed: bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This module does not come built-in with
2 min read
Extract title from a webpage using Python
Prerequisite Implementing Web Scraping in Python with BeautifulSoup, Python Urllib Module, Tools for Web Scraping In this article, we are going to write python scripts to extract the title form the webpage from the given webpage URL. Method 1: bs4 Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This module does n
3 min read
Reading selected webpage content using Python Web Scraping
Prerequisite: Downloading files in Python, Web Scraping with BeautifulSoup We all know that Python is a very easy programming language but what makes it cool are the great number of open source library written for it. Requests is one of the most widely used library. It allows us to open any HTTP/HTTPS website and let us do any kind of stuff we norm
3 min read
Create Basic Webpage with Pyscript
In this article, we will cover how we can create a basic webpage with Pyscript The Pyscript is a python framework/library that allows us to write python code inside HTML directly with the help[ of pyscript tag. Pyscript is a new project and is currently under development, but it has gained a lot of attention recently. It can make a page generate dy
5 min read
Selenium Python Tricks
Selenium: Selenium Python bindings provide a convenient API to access Selenium Web Driver like Firefox, Chrome, etc. What is webdriver? Selenium WebDriver is an automation testing tool. When I say automation, it means it automates test scripts written in Selenium. Webdriver Install Chrome: https://sites.google.com/a/chromium.org/chromedriver/downlo
3 min read
Selenium Base Mini Project Using Python
Project Description:-Here, we're going to study a simple SMS bomber trick (for amusing and educational purpose). Selenium is a free tool for automated trying out across exceptional browsers. In this tutorial, we will learn how to ship mechanically range of unsolicited mail SMS for given variety of frequency and interval. Requirement: You need to in
2 min read
Practice Tags :