Friday, September 29, 2017

Selenium Tips and Tricks- X,Y Coordinate of Element, Position Function

1. How to get X Y coordinates of any element in selenium ?

    By using Point class we can get it. Example -

    WebElement ele = driver.findElement(By.xpath("xpath of element"));

     Point p = ele.getLocation();

// For x coordinate
     int xc=p.getX();

// For Y coordinate
     int yc=p.getY();


2. How to click on any checkbox using last() and position() function ?

    Sometime checkbox has not any identifier then we are used absolute xpath or last and position function to identify it. Suppose there are so many checkboxes on one page and I want to click on 2nd number checkbox then by using below trick we can identify.

  xpath= (//input[@type='checkbox'])[position()=2]

//If I want to click on last checkbox

 xpath=  (//input[@type='checkbox'])[last()]

//If I want to click on 2nd last checkbox
    
 xpath=  (//input[@type='checkbox'])[last()-1]


3. How to get first selected value from a Dropdown ?

    Using getFirstSelectedOption() method.

   Select ele = new Select (driver.findElement(By.xpath("xpath of dropdown")));

  String firstoption = ele.getFirstSelectedOption();


      

No comments:

Post a Comment