Interacting with Webpage Selenium Python

The Python Framework for Selenium is designed to do automatic testing for Python. The Selenium Python bindings include a simple API for writing Selenium WebDriver functional/acceptance checks. Check out – Navigating links using the get form – Selenium Python to learn how to access a website using Selenium Python. Getting the opportunity to travel somewhere isn’t that helpful. What we actually want to do is connect with the websites, or more accurately, the HTML components that make up a website. First and foremost, we must locate one. WebDriver includes a range of alternatives for finding elements. As an example, consider the following element:

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

One of the locating techniques must be used to locate an artefact. As an example,

element = driver.find_element_by_name(“password”)

element = driver.find_element_by_id(“password-id”)

element = driver.find_element_by_xpath(“//input[@id=’password-id’]”)

We can also use to locate multiple components.

elements = driver.find_elements_by_name(“passwd”)

A link can also be identified by looking at its text, but be wary! It has to be an exact match! When using XPATH in WebDriver, one should be vigilant. Only the first element that matches the query would be returned if there are several elements that match the query. If no such factor can be found, a NoSuchElementException is hurled.

Learn More Python Tutorial

In WebDriver’s “Attribute” API, we define all types of elements using the same interface. This means that while the auto-complete key combination in the IDE will show a large number of potential methods, not all of them would make sense or be accurate. To be clear that all procedures are accurate,

So, once you’ve obtained an ingredient, what’s next? For eg, one may want to insert text into a field.

element.send_keys(“some text”)

The “Keys” class can be used to simulate clicking the arrow keys:

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

It’s also worth noting that send keys can be called on any feature, allowing you to test keyboard shortcuts like those found in Gmail.

The transparent approach makes it possible to clear the contents of a text region or textarea:

element.clear()

Example:

Let’s see how we can use Google to search for anything automatically.

Code
	# importwebdriver
	from selenium import webdriver
	# create webdriver object
	driver = webdriver.Firefox()
	# get google.co.in
	driver.get("https://www.google.co.in/")
	# get element 
	element = driver.find_element_by_id("gsc-i-id1")
	# send keys 
	element.send_keys("Selenium Tutorial")
	
Output:
Interacting with Webpage Selenium Python
Example2:

Let’s see how we can use Wikipedia to search for anything automatically.

Code
	# importwebdriver
	from selenium import webdriver
	# create webdriver object
	driver = webdriver.Firefox()
	# get google.co.in
	driver.get("https://www.wikipedia.org/")
	# get element 
	element = driver.find_element_by_id("gsc-i-id1")
	# send keys 
	element.send_keys("Selenium")
	
Output:
Interacting with Webpage Selenium Python
Enroll Yourself: Python Training

Copyright 1999- Ducat Creative, All rights reserved.