Automation QA Testing Course Content

WebDriver Locators

Webdriver Locators:
 id("idvalue")
 name("namevalue")
 className("classvalue")
 xpath("xpathvalue")
 cssSelector("cssvalue")
 tagName("tag")
 linkText("exactlinkname")
 partialLinkText("partiallinkname")
 -----------------------------------------------------------------------------------------
 all the locators are static methods in By class


 WebElement el=driver.findElement(By.locator("locatorvalue"));

 identifying element by id

 WebElement elename=driver.findElement(By.id("idvalue"));
 ex:
 WebElement email_editbox=driver.findElement(By.id("login-email"));

 identifying element by name
 WebElement elename=driver.findElement(By.name("namevalue"));
 ex:
 WebElement pwd_editbox=driver.findElement(By.name("session_password"));

 //fetching all the elements into collection using
 findElements API when name is common to many sub elements
 List<WebElement>collectionname=parentElement.findElements(By.name("namevalue"));

  //using findElements for the browser
  List<WebElement>collectionname=driver.findElements(By.name("namevalue"));

 by className
 //identifying the element by className locator
 WebElement elename=driver.findElement(By.className("classvalue"));
 ex:
 WebElement linkedin_logo=driver.findElement(By.className("lazy-loaded"));

 //fetching all the elements with class from parentelement
List<WebElement>collectionname=parentElement.findElements(By.className("classvalue"));

 //fetching all the elements with class in the browser
List<WebElement>collectionname=driver.findElements(By.className("classvalue"));

 By xpath
 //identifying the element by xpath
 WebElement elename=driver.findElement(By.xpath("xpathvalue"));

 Ex:WebElement elename=driver.findElement(By.xpath("//*[@value='Sign in']"));
 //fetching all the elements by xpath from parentElement
 List<WebElement>collectionname=parentElement.findElements(By.xpath("commonxpathvalueforAllChildElements"));

 //fetching all the elements by xpath in a browser
 List<WebElement>collectionname=driver.findElements(By.xpath("xpathvalue"));

 By cssSelector
 identifying the element by cssSelector
 WebElement elename=driver.findElement(By.cssSelector("cssvalue"));
 Ex:
 WebElement linkedinlogo=driver.findElement(By.cssSelector("img[alt='LinkedIn']"));

 //finds all the elements using cssSelector from parentElement
 List<WebElement>collectionname=parentElement.findElements(By.cssSelector("commoncssvalue"));
 //finds all the elements using cssSelector in entire browser
 List<WebElement>collectionname=driver.findElements(By.cssSelector("cssvalue"));

 By tagName
 //identifying single element using tagName locator
 WebElement elename=driver.findElement(By.tagName("tag"));
 //fetching all the child elements from parent element
 List<WebElement>collectionname=parentElement.findElements(By.tagName("tag"));
//fetching all the elements that are having same tag in entire page
 List<WebElement>collectionname=driver.findElements(By.tagName("tag"));

 1)how do you know no. of links in webpage.
 //fetch all the page links into List type collection
 List<WebElement>lnks=driver.findElements(By.tagName("a"));

 //size of the collection
 System.out.println(lnks.size());

 2)//how can you fetch particular section links in webpage.
 //identify the section
 WebElement sectionname=driver.findElement(By.locator("locatorvalue"));

 //fetch all the links in the section
 List<WebElement>seclnks=sectionaname.findElements(By.tagName("a"));

 System.out.println(seclnks.size());

 3)//how can you fetch particular dropdown options

 //identify the dropdown
 WebElement drpname=driver.findElement(By.localtor("locatorvalue"));

 //fetch all the options using findElements API and tagName locator (option)and store in List type collection
 List<WebElement>opts=drpname.findElements(By.tagName("option"));


 //how to identify the link
 By linkText
 WebElement elename=driver.findElement(By.linkText("exactlinkname"));
 Ex:
 WebElement forgotpassword_link=driver.findElement(By.linkText("Forgot password?"));

 By partialLinkText
 WebElement elename=driver.findElement(By.partialLinkText("partiallinkname"));
 Ex:
 WebElement dateofbirth_link=driver.findElement(By.partialLinkText("date of birth"));
-----------------------------------------------------------------------------------------------
Create a dynamic XPath which can find all headlines on Amazon.com 
Steps - 
Go to Amazon.com
Search for Smart TV
Create Xpath to retrieve the name of all products and store in List?

Ans)
To create a dynamic XPath to retrieve the names of all products on Amazon.com after searching for "Smart TV," you can follow these steps using Selenium in Java:
  1. Import the necessary Selenium libraries and set up a WebDriver to open Amazon.com.

  2. Search for "Smart TV" on Amazon.

  3. Create an XPath expression to locate all the product names on the search results page.

  4. Use the XPath to retrieve the product names and store them in a List.

  5. Here's an example Java code to achieve this:

  6. import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.List; import java.util.concurrent.TimeUnit; public class AmazonSmartTVNames { public static void main(String[] args) { // Set the path to the ChromeDriver executable System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe"); // Initialize the WebDriver WebDriver driver = new ChromeDriver(); // Navigate to Amazon.com driver.get("https://www.amazon.com/"); // Maximize the browser window (optional) driver.manage().window().maximize(); // Search for "Smart TV" driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Smart TV"); driver.findElement(By.id("nav-search-submit-button")).click(); // Wait for the search results to load (adjust the timeout as needed) driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Create an XPath to locate the product names String xpathExpression = "//span[@class='a-size-medium a-color-base a-text-normal']"; // Find all elements matching the XPath List<WebElement> productElements = driver.findElements(By.xpath(xpathExpression)); // Create a List to store the product names List<String> productNames = productElements.stream().map(WebElement::getText).toList(); // Print the product names for (String productName : productNames) { System.out.println(productName); } // Close the WebDriver driver.quit(); } }

  7. Make sure to replace "path/to/chromedriver.exe" with the actual path to your ChromeDriver executable. Additionally, you may need to adjust the XPath expression and any other elements' locators based on the current structure of Amazon's website.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.