Last active
August 18, 2019 09:24
-
-
Save muhammedfurkan/30603ee91571cb6d8d3140c811c0bfbb to your computer and use it in GitHub Desktop.
Python Selenium Locate Elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 3. Locate Elements By Link Text | |
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(5) | |
elem = driver.find_element_by_link_text("Python Tutorial") | |
elem.click() | |
time.sleep(20) | |
driver.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 4. Locate Elements By Partial Link Textfrom 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(5) | |
elem = driver.find_element_by_partial_link_text("Python") | |
elem.click() | |
time.sleep(20) | |
driver.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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