Selenium Locators : Naveen Automation Labs

PHOTO EMBED

Sat Nov 13 2021 03:35:18 GMT+0000 (UTC)

Saved by @jpannu #linux #server

//Locators 

   > driver.findElement();
    - Return value of the above code is always 'WebElement'.

1. ID
  > findElement(By.id("idNameHere"));

2. Name
> driver.findElement(By.name('valueHere'));  
- it's the attribute 'name' in the element.

3. Xpath  ( Extended HTML path )

  > //div /* It will select the all the div elements */
  > //div[@attribute='value'] /* It will select the all the div elements with specific attribute and value */
  
  > //div[@type='submit' and @value='Login']
  > //div[@type='submit' and @value='Login' and @name='Login']
  
  > //a[text()='Gmail']
  > //a[text()='Text to Match']
              //OR use 'contains'                    -- Recommended. 
  > //a[contains(text(),'Gmail')]                      
  > //a[contains(text(),'Text to Match')]
  
 //div[@class='bd-example']//button[@type='button' and @class='btn btn-dark']
  /*  **We have to mention all the active classes element( if we are searching using the class attribute )           */
  
  > //div[@class='bd-example']//input[@value='Reset']
   div.bd-example > input 
  
4. CSS selector
 > driver.findElement(By.cssSelector("#idHere"));
 > driver.findElement(By.cssSelector(".classNameHere"));

5. LinkText : only for links text
 > driver.findElement(By.linkText("Trouble signing in?"));
//This will look for the link TEXT. Not the actual link. 

6. Partial Link Text
 > driver.findElement(By.partialLinkText("Trouble signi"));
//Only the partial string is passed. 
content_copyCOPY

https://www.youtube.com/watch?v=X9eLWkZBKgQ