st mod 5
st mod 5
the
command.
• Opening websites
• Clicking links
• Filling forms
• Navigating pages
• WebDriver can also examine web pages to check elements and make decisions based on what it sees.
• Its main use is for automated testing of websites.
• Earlier, regression tests required manual effort by developers or testers to:
WebDriver works in all major browsers and with all major programming languages.
CSS Selectors
• CSS Selectors are powerful and concise for locating elements using CSS rules.
• Syntax is inspired by CSS styling rules.
Examples
1. By Tag and ID:
python
Copy code
element = driver.find_element_by_css_selector("input#username")
o Locates an <input> element with the ID username.
2. By Tag and Class:
python
Copy code
element = driver.find_element_by_css_selector("div.form-control")
o Locates a <div> element with the class form-control.
3. By Attribute:
python
Copy code
element = driver.find_element_by_css_selector("input[type='text']")
o Locates an <input> element with the type attribute equal to text.
4. By Hierarchy:
python
Copy code
element = driver.find_element_by_css_selector("div > span")
o Locates a <span> element that is a direct child of a <div>.
Advantages
• Fast and easy to read.
• Supported directly by browsers.
• Shorter syntax compared to XPath.
Limitations
• Cannot traverse back up the DOM (no parent navigation).
XPath
• XPath is a query language for XML-like structures, used to locate elements with precision in HTML.
Examples
1. By Absolute Path:
element = driver.find_element_by_xpath("/html/body/div/form/input")
o Locates the element by traversing the complete DOM hierarchy (not recommended).
2. By Relative Path:
element = driver.find_element_by_xpath("//input[@id='username']")
element = driver.find_element_by_xpath("//button[text()='Submit']")
element = driver.find_element_by_xpath("//div[@class='form-group']/input")
o Locates an <input> child element inside a <div> with the class form-group.
4) Illustrate different strategies for Locating Elements on the web page using Selenium web driver
with an examples.
Here’s a clear and structured illustration of various strategies for locating elements on a webpage using
Selenium WebDriver, with examples:
1. By ID
Example:
element = driver.find_element_by_id("username")
2. By Class Name
• Targets elements using their class attribute.
• Best for: Elements sharing common styles.
Example:
element = driver.find_element_by_class_name("form-control")
3. By Name
Example:
element = driver.find_element_by_name("email")
4. By Tag Name
Example:
element = driver.find_element_by_tag_name("button")
5. By Link Text
• Locates anchor (<a>) elements using the visible text in the link.
• Best for: Navigation links.
Example:
element = driver.find_element_by_link_text("Home")
• Similar to Link Text, but matches only part of the text in the link.
• Best for: Links with dynamic or lengthy text.
Example:
element = driver.find_element_by_partial_link_text("Learn")
7. By CSS Selector
• Uses CSS rules to locate elements.
• Best for: Precise and concise element location.
Examples:
• Locate by ID:
element = driver.find_element_by_css_selector("#username")
• Locate by class:
element = driver.find_element_by_css_selector(".form-control")
• Locate by attribute:
element = driver.find_element_by_css_selector("input[type='email']")
8. By XPath
Examples:
• Locate by attribute:
element = driver.find_element_by_xpath("//input[@id='username']")
• Locate by text:
element = driver.find_element_by_xpath("//button[text()='Submit']")
element = driver.find_element_by_xpath("//div[@class='form-group']/input")
9. By DOM (JavaScript)
Example:
6) Write and test a program to provide total number of objects present available on
import unittest
class Count(unittest.TestCase):
def setUp(self):
installed
self.driver.get("http://www.google.co.in")
self.driver.maximize_window()
def test_count_elements(self):
driver = self.driver
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()