Monday, 14 May 2018

xpath

Xpath tips:

Syntex    Xpath=//tagname[@attribute='value']
install browser extension SelectorsHub by sanjay  

driver.findElement(By.Xpath=//input[@type='submit' AND @name='btnLogin']
driver.findElement(By.Xpath("//input[@type='text']
driver.findElement(By.xpath(".//input[@placeholder='Search by Product Name, SKU or SUPC']")).sendKeys(QUANTITY);
driver.findElement(By.Xpath=//input[@value='RESET']
driver.findElement(By.xpath("//input[@id='inventory']")).clear();
driver.findElement(By.xpath("//*[contains,'Skip']"))
driver.findElement(By.xpath("//div[@class='modal-table-cell']//span[text()='Skip']")).size()
driver.findElement(By.xpath(".//img[@src='/static/sfui/assets/images/snapdeal_logo_tagline.png']")).click();
driver.findElement(By.Xpath=//*[@class='barone']
driver.findElement(By.xpath("//*[@class ='hide-on-mobile']//*[text()='Khakee1']")).click()
driver.findElement(By.linkText("Logout")).click();
driver.findElement(By.Xpath=//label[@id='message23']
driver.findElement(By.Xpath=//a[@href='http://demo.guru99.com/']
driver.findElement(By.Xpath=//img[@src='//cdn.guru99.com/images/home/java.png']


-----------------------------------------Radio button starts-----------------------------
Radio button selection Problem : 
<label class="radio-inline">
       <input type="radio" name="newsletter" value="1">Yes</label>
<label class="radio-inline">
       <input type="radio" name="newsletter" value="0" checked="checked">No</label>

Radio button selection Problem 4 solutions :
//input[@name='newsletter'][@value='1']
//input[@name='newsletter' and @value='1'] 
//label[normalize-space()='Yes']
(//input[@name='newsletter'])[1]                            ==> () bracket is mandatory here
----------------------------------------Radio button ends-------------------------------


Xpath=//*[contains(text(),'here')]
Xpath=//*[contains(@href,'guru99.com')]


@FindBy(xpath="//input[@type='submit']")
WebElement loginBtn;

@FindBy(xpath="//button[contains(text(),'Sign Up')]")
WebElement signUpBtn;

@FindBy(xpath="//img[contains(@class,'img-responsive')]")
WebElement LogoImg;


followingà  //*[@attribute=’value’]//following::tagname

precedingà  //*[@attribute=’value’]//preceding::tagname

ancestor à  //*[@attribute=’value’]//ancestor::tagname

descendantà  //*[@attribute=’value’]//descendant::tagname

childà  //*[@attribute=’value’]//child::tagname

parentà  //*[@attribute=’value’]//parent::tagname

sibling  à //*[@attribute=’value’]/following-sibling::tagname


Descendant Example: 

<div id="search" class="input-group">

  <input type="text" name="search" value="" placeholder="Search" class="form-control input-lg">

  <span class="input-group-btn">

    <button type="button" class="btn btn-default btn-lg">

        <i class="fa fa-search"></i>

    </button>

  </span>

</div>


Xpath =>   //div[@id='search']/descendant::i


How to define any element :

By userName = By.id("userName");                     ==> Best


@FindBy(xpath="//span[text()='Contacts']")

WebElement ContactsLink;

Dynamic Xpath :

//button[@id='Order_Number']

//button[contains(@id,'Order_Number')]

//button[starts-with(@id,'Order_Number')]

//form[@data-testid='royal_login_form']/descendant::button  ==> using Xpath Axes

//button[contains(text(),'Log in')]                        ==> using Xpath Axes

//button[1]                                ==> using index

No comments:

Post a Comment