XPath: Selectors QTips

  • if there are more than one matches either
    • an array will be passed pack ( as in the case with JQuery  OR WebDriver getElements )
    • the first item matched will be returned ( as in the case with WebDriver getElement )
  • to specify only ONE element, use an array pointer [0], [1], etc
  • OR add more specificity to the XPath string
  • matching on element id will return ONE element ( as with //input[@id="LoginName"]
  •  

 

  • / = absolute reference
  • // = relative reference
  • //input[@id="LoginName"] = id attribute used to match element
  • //*/div/a = relative reference/<any element>/<div element>/<anchor element>
  • //**/div/a = relative reference/<any number of elements>/<div element>/<anchor element>
  • //a[@id="Link Text]/../label = achor tag SIBLING
  • //a[@id="Link Text]/../../label = achor tag PARENT'S SIBLING
  • //a[contains(.,"Link Text")] = <a href=#>Link Text</a>
    • the '.' in contains denotes 'text()' or /value
    • (MIGHT also parse attributes - need to verify)
  • JVM WebDriver example for 'hidden' objects
    • By hiddenObj = By.xpath("//*[@id='idOfYourHiddenElement' and @type='hidden']");
  •  

 

 


Investigate & Test These:

translate(normalize-space(@style), ' ', '')


This should work:

.//button[.='OK' and not(ancestor::div[contains(@style,'display:none')])
and not(ancestor::div[contains(@style,'display: none')])]

EDIT:

The simpler and more efficient expression below:

//div[not(contains(@style,'display:none'))]//button[.='OK']

does not work properly because every button has at least one div that's visible in its ancestors.


Note class="display: none;" isn't matched by the above (the space makes it different)

 

 

 

Tags