locators:
ID, Xpath, CSSSelector ,Classname, name, linkText
Below is example of Name and ID fields which you retrieved from inspecting on browser from elements section.
driver.get("https://rahulshettyacademy.com/angularpractice/") driver.find_element(By.NAME,"email").send_keys("hello@gmail.com") ## fetched from name attribute of html element. driver.find_element(By.ID,"exampleInputPassword1").send_keys("122324") ## name not defined, so using ID as it is also unique.
driver.find_element(By.ID,"exampleCheck1").click()
video demo
from selenium import webdriver
from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By service_obj = Service() # Configure Brave options brave_options = webdriver.ChromeOptions() brave_options.binary_location = r"C:\Users\UReddy\AppData\Local\BraveSoftware\Brave-Browser\Application\brave.exe" driver=webdriver.Chrome(service=service_obj, options=brave_options) driver.maximize_window() driver.get("https://rahulshettyacademy.com/angularpractice/") driver.find_element(By.NAME,"name").send_keys("Randome Name") driver.find_element(By.NAME,"email").send_keys("hello@gmail.com") ## fetched from name attribute of html element. driver.find_element(By.ID,"exampleInputPassword1").send_keys("122324") ## name not defined, so using ID as it is also unique. driver.find_element(By.ID,"exampleCheck1").click() #driver.close()
##if no name or id provided or they are not unique or not constant, can go with xpath.
## //tagname[@attribute='value'] --> //input[@type='submit']
driver.find_element(By.XPATH, "//input[@type='submit']").click() message=driver.find_element(By.CLASS_NAME,"alert-success").text print(message) assert "Success" in message
Full code with xpath,classname
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By service_obj = Service() # Configure Brave options brave_options = webdriver.ChromeOptions() brave_options.binary_location = r"C:\Users\UReddy\AppData\Local\BraveSoftware\Brave-Browser\Application\brave.exe" driver=webdriver.Chrome(service=service_obj, options=brave_options) driver.maximize_window() driver.get("https://rahulshettyacademy.com/angularpractice/") ##driver.find_element(By.NAME,"name").send_keys("Randome Name") driver.find_element(By.CSS_SELECTOR,"input[name='name']").send_keys("Randome Name") driver.find_element(By.NAME,"email").send_keys("hello@gmail.com") ## fetched from name attribute of html element. driver.find_element(By.ID,"exampleInputPassword1").send_keys("122324") ## name not defined, so using ID as it is also unique. driver.find_element(By.ID,"exampleCheck1").click() driver.find_element(By.XPATH, "//input[@value='Submit']").click() message = driver.find_element(By.CLASS_NAME, "alert-success").text print(message) assert "Success!" in message driver.close()
video link
Can change the xpath for input fields above with CSS selector also.
##CSS selector - tagname[attribute='value']
##can get css selector or xpath query using chropath plugin.
selectorshub chrome plugin, to validate xpath/css typed.
can generate css selectors above using id #id or class .classname
So when using ID field.
using id directly: driver.find_element(By.ID,"inlineRadio1").click()
using css selector: driver.find_element(By.CSS_SELECTOR,"#inlineRadio1").click()
using xpath selector: driver.find_element(By.XPATH,"#inlineRadio1").click()
if we get multiple values matching, can index them.
(//input[@type='text'])[3]
more testing: rahulshettyacademy.com/client
driver.find_element(By.LINK_TEXT,"Forgot password?").click()
driver.find_element(By.PARTIAL_LINK_TEXT,"Forgot").click()
##xpath can go from parent to child.. //parentelement/childelement By.XPATH
//form/div[1]/input
using css: By.CSS_SELECTOR
form div:nth-child(2) input
//xpath based on text.
//button[text()='Save New Password']
rahulshettyacademy.com/#/practice-project
No comments:
Post a Comment