Home page

Showing posts with label Locator. Show all posts
Showing posts with label Locator. Show all posts

Friday, October 18, 2019

Get count of all the links in web page

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class GetLinksCount {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"//drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.flipkart.com/");
String newwindow = driver.getWindowHandle();
driver.switchTo().window(newwindow);
driver.findElement(By.xpath("//button[@class='_2AkmmA _29YdH8']")).click();

List<WebElement> links = driver.findElements(By.tagName("a")); //This will store all links in links object.

System.out.println(links.size()); //Use size method to get count

driver.quit();
}

}

Thursday, September 19, 2019

How to use CSSSelector in selenium webdriver ?

How to locate webelement using CSSSelector in selenium webdriver ?
  1. <Tag Name>#<[id='value']> for search with id
  2. <Tag Name>.<[classname='value']> for search with classname
  3. <Tag Name><[Attributename*='value']> for partial search
  4. <Tag Name><[Attributename^='value']> for start with
  5. <Tag Name><[Attributename$='value']> for ends with
  6. <Tag Name><[Attributename='value'] for exact content match of single attribute
  7. <Tag Name><[Attributename1='value'][Attributename2='value'] for exact content match of more than one attributes
  8. ParentTagName > ChildTagName[AttributeName='value'] for finding through unique parent. Use greater than sign.
  9. ParentTagName[AttributeName='value'] > TagName for finding direct child
  10. ParentTagName[AttributeName='value'] TagName for finding all direct and indirect child(Sub child)
  11. nth-child(n): Example is .classname > TagName:nth-child(1)
  12. nth-last-child(n): Example is .classname > TagName:nth-last-child(1)
  13. first-child: Example is .classname > TagName:first-child
  14. last-child: Example is .classname > TagName:last-child
  15. Sibling: Example is .classname > TagName:nth-child(1) + TagName
  16. All sibling:  Example is .classname > TagName:nth-child(1) ~ TagName
  17. Identify checked checkbox: Example is <TagName>:checked
If you have following questions then ask in comment. I'll answer on that so you can understand CSSSelector concept:
  1. What is Tag Name?
  2. What is Attributename?
  3. What is value?
  4. What is Parent Tag?
  5. What is Sibling
  6. What is Child?