Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save muhammedfurkan/30603ee91571cb6d8d3140c811c0bfbb to your computer and use it in GitHub Desktop.
Save muhammedfurkan/30603ee91571cb6d8d3140c811c0bfbb to your computer and use it in GitHub Desktop.
Python Selenium Locate Elements
## 8. Locate Elements By Classname
<html>
<body>
<div class="round-button">Click Here</div>
</body>
<html>
get_div = driver.find_element_by_class_name('round-button')
## 6. Locate Elements By CSS Selector
<html>
<body>
<div class="round-button">Click Here</p>
</body>
<html>
div.round-button
get_div = driver.find_element_by_css_selector('div.round-button')
## 2. Locate Elements By ID
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://google.com")
driver.maximize_window()
time.sleep(5)
inputElement = driver.find_element_by_name("q")
inputElement.send_keys("Techbeamers")
inputElement.submit()
time.sleep(20)
driver.close()
## 1. Locate Elements By Name
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://google.com")
driver.maximize_window()
time.sleep(5)
inputElement = driver.find_element_by_name("q")
inputElement.send_keys("Techbeamers")
inputElement.submit()
time.sleep(20)
driver.close()
## 7. Locate Elements By Tagname
<html>
<body>
<title>Hello Python</title>
<p>Learn test automation using Python</p>
</body>
<html>
get_div = driver.find_element_by_tag_name('title')
## 5. Locate Elements By Xpath
<html>
<body>
<form id="signUpForm">
<input name="emailId/mobileNo" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="SignUp" />
<input name="continue" type="button" value="Clear" />
</form>
</body>
<html>
form_element = driver.find_element_by_xpath("/html/body/form[1]")
form_element = driver.find_element_by_xpath("//form[1]")
form_element = driver.find_element_by_xpath("//form[@id='signUpForm']")
email_input = driver.find_element_by_xpath("//form[input/@name='emailId/mobileNo']")
email_input = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")
email_input= driver.find_element_by_xpath("//input[@name='emailId/mobileNo']")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment