Locating single elements in Selenium Python

In Selenium Python, Locators Techniques are methods for finding entities on a website and executing operations on them. 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. After downloading Selenium and testing out Browsing links using the get process, you might want to experiment with Selenium Python further. After opening a page with selenium, such as ducatindia, the user may choose to simplify the process of clicking buttons, filling out forms, or performing other activities. The focus of this article is on discovering single elements in Selenium Python.

Locator Methods for Locating Single First Elements

Selenium is a mineral that is present in nature. Python uses a number of element-location techniques. A variable can be found in eight different forms. In Python, below is a list of Selenium finding techniques.

Locators Description
find_element_by_xpath The first variable matching the location’s xpath syntax will be returned.
find_element_by_partial_link_text A partial link text value that fits the position will be contained in the first entity recovered.
find_element_by_class_name The first entity with a class attribute name that fits will be returned.
find_element_by_id The first entity with the location’s id attribute value will be returned.
find_element_by_link_text The relationship text value for the first element returned would match the position.
find_element_by_css_selector The first variable with a CSS selector that matches will be returned.
find_element_by_tag_name The first person with the given tag name will be restored.
find_element_by_name The first entity with a name attribute value that corresponds to the position is returned.
Learn More Python Tutorial
find_element_by_id

The first entity with an id attribute value that fits the location will be returned using this technique. A NoSuchElementException will be thrown if no element has a matching id attribute.

Syntax:

driver.find_element_by_id(“id_of_element”)

Example:

Consider the following page source:

	< html>
	< body>
	< form id="loginForm">
	< input name="username" type="text" />
	< input name="password" type="password" />
	< input name="continue" type="submit" value="Login" />
	< /form>
	< /body>
	< /html>
	

After you’ve built a driver, you can use it to catch an aspect.

login_form = driver.find_element_by_id(‘loginForm’)

find_element_by_xpath

The first entity with an xpath pattern that fits the position will be returned using this technique. A NoSuchElementException would be tossed if no variable has a corresponding element attribute.

Syntax:

driver.find_element_by_xpath(“xpath”)

Example:

Consider the following page source:

	< html>
	< body>
	< form id="loginForm">
	< input name="username" type="text" />
	< input name="password" type="password" />
	< input name="continue" type="submit" value="Login" />
	< /form>
	< /body>
	< /html>
	

After you’ve built a driver, you can use it to catch an aspect.

login_form = driver.find_element_by_xpath(“/html/body/form[1]”)

login_form = driver.find_element_by_xpath(“//form[2]”)

find_element_by_name

The first variable with a name attribute value that fits the position will be returned using this technique. A NoSuchElementException would be thrown if no element has a name attribute that fits.

Syntax:

driver.find_element_by_name(“name_of_element”)

Example:

Consider the following page source:

	< html>
	< body>
	< form id="loginForm">
	< input name="username" type="text" />
	< input name="password" type="password" />
	< input name="continue" type="submit" value="Login" />
	< /form>
	< /body>
	< /html>
	
find_element_by_link_text

The first variable with a relation text value that matches the location will be returned using this technique. A NoSuchElementException will be thrown if no element has a matching relation text attribute.

Syntax:

driver.find_element_by_link_text(“Text of Link”)

Example:
Code:
	< html>
	< body>
	< p>Are you sure you want to do this?< /p>
	< a href="continue.html">Continue< /a>
	< a href="cancel.html">Cancel< /a>
	< /body>
	< /html>
	

After you’ve built a driver, you can use it to catch an aspect.

login_form = driver.find_element_by_link_text(‘Continue’)

find_element_by_tag_name

The first entity with the given tag name will be returned using this technique. A NoSuchElementException would be tossed if no element has a tag name that suits.

Syntax:

driver.find_element_by_tag_name(“Tag name”)

Example:
Code:
	< html>
	< body>
	< h1>Welcome< /h1>
	< p>Site content goes here.< /p>
	< /body>
	< /html>
	

After you’ve built a driver, you can use it to catch an aspect.

login_form = driver.find_element_by_tag_name(‘h1’)

find_element_by_partial_link_text

The first entity with a partial relation text value that matches the location will be returned using this technique. A NoSuchElementException will be thrown if no element has a matching partial relation text attribute.

Syntax:

driver.find_element_by_partial_link_text(“Text of Link”)

Example:
Code:
	< html>
	< body>
	< p>you sure you want to do this task< /p>
	< a href="continue.html">Contain< /a>
	< a href="cancel.html">Cancel< /a>
	< /body>
	< /html>
	

After you’ve built a driver, you can use it to catch an aspect.

login_form = driver.find_element_by_partial_link_text(‘Conti’)

find_element_by_css_selector

The first element with the corresponding CSS selector will be returned using this technique. A NoSuchElementException will be thrown if no CSS selector matches the feature.

Syntax:

driver.find_element_by_css_selector(“CSS Selectors”)

Example:
Code:
	< html>
	< body>
	< p class="content">website content here.< /p>
	< /body>
	< /html>
	

After you’ve built a driver, you can use it to catch an aspect.

content = driver.find_element_by_css_selector(‘p.content’)

find_element_by_class_name

The first entity with the corresponding class attribute name will be returned using this technique. A NoSuchElementException will be thrown if no element has a corresponding class element name.

Syntax:

driver.find_element_by_class_name(“class_of_element”)

Example:
Code:
	< html>
	< body>
	< p class="content">Site data here.< /p>
	< /body>
	< /html>
	

After you’ve built a driver, you can use it to catch an aspect.

content = driver.find_element_by_class_name(‘content’)

Enroll Yourself: Python Training

Copyright 1999- Ducat Creative, All rights reserved.