XPath functions for dynamic XPath in Selenium

We invite future students of the "Java QA Automation Engineer" course and all those interested to watch a demo-lesson in the format of an open webinar.



We also share the translation of a useful article.










XPath .





, - . , , ID, Name, Link, Class, , , .





,  () .





=>  Selenium .





[]





XPath

, .





, , ID, Name, Class Link, ?





:





 Twitter





, «Log in to Twitter» . , ID



, Class



, Link



Name



, .





Firepath Firefox XPath:





//[@id=’page-container’]/div/div[1]/h1





XPath, id . XPath, , , . , XPath , ID, Class, Link Name.









XPath

«Log in to Twitter», XPath



.





  1. contains() [ ]





  2. starts-with() [ ]





  3. text()





XPath



, contains()



, starts-with()



text()



, «Log in to Twitter» , .





1. Contains()

.  - «Log in to Twitter», XPath contains()



.





:





  • //h1[contains(text(),’ Log in to’)]







  • //h1[contains(text(),’ in to Twitter’)]







. -.





, contains()



. , . contains()



, , .





, «Log in to Twitter» contains()



.





2. starts-with()

.  - «Log in to Twitter», XPath starts-with().





:





  • //h1[starts-with(text(),’Log in’)]







  • //h1[starts-with(text(),’Log in to’)]







, XPath- starts-with()



(«Log») . , .





, «Log in to Twitter» starts-with()



.





XPath starts-with()



: //h1[starts-with(text(),’in to Twitter’)]







.  , - .





3. text()

.  - «Log in to Twitter», XPath



text()



.





, <h1>



</h1>



. text()



, contains()



starts-with()



, .





Xpath text()



:





XPath

XPath



(contains



starts-with



) , . «@



».









 Google





1. Contains()

.  «I’m Feeling Lucky» (« ») XPath- contains()



, .





 â€” Value







  • //input[contains(@value,’Feeling’)]







  • //input[contains(@value,’Lucky’)]







, Value



«Feeling» «Lucky» contains()



. , , Value



, .





— Name







//input[contains(@name=’btnI’)]







XPath :





contains()



starts-with()



. , .





type



«I'm Feeling Lucky», XPath .





, . type



.





2. starts-with()

starts-with()



, , . , . .





 Facebook.





First Name () Surname () .





First Name .





Surname .





, Facebook, id



.





First Name id="u02"





Surname id="u04"





, starts-with()



, id



. , . id



, «u0».





Starts-with() [ id]





//input[starts-with(@id,"u0")]







.  . starts-with



.





11 , XPath , id



«u0». id



(«2» , «4» . .) .





starts-with



, , , .





starts-with



.









/ <strong>Generic Method</strong> /





 





public void xpathLoc(String identifier){





    //The below step identifies the element “First Name” uniquely when the argument is “2”





WebElement E1=d1.findElement(By.xpath("//input[starts-with(@id,”u0”+identifier )]"));





E1.sendKeys(“Test1”);  / This step enters the value of First Name as “Test 1” /





}





/ <strong>Main Method</strong>*/





 





public static void main(String[] args) {





        xpathLoc(“2”); --- This step calls the xpathLoc() method to identify the first name.





        }





. Eclipse . , , XPath.





. , , ( ), .





, XPath contains()



, starts-with()



text()



HTML DOM.





XPath:

  1. contains()



    XPath, .





  2. starts-with()



    XPath, .





  3. contains()



    starts-with()



    .





  4. text()



    XPath, .





  5. text()



    .





  6. starts-with()



    , XPath .





, XPath XPath -.






, . , 100 ?





.



"Java QA Automation Engineer"
.












All Articles