5

I am trying to make webdriver/browser to be able to process XPATH version 2.0.

Using old version of XPATH is fine, but at certain specific case I will also need to process XPATH version 2.0.

I tried some research regarding this but still no clue.

For example, say I want to get all <a> elements that contain title or href attribute:

# find elements by xpath in older version
driver.find_elements_by_xpath("//a[@title or @href]")

# I want the code to be able to process XPATH version 2.0
driver.find_elements_by_xpath("//a/(@title|@href)")

I expect I can find some configuration (or what browser should I use) to achieve availability of processing XPATH version 2.0.

5
  • This will select the attributes not the elements. Is that what you intended? Mar 24, 2019 at 1:01
  • @pguardiario I mean to select the elements that contains those attribute. so it will not retrieve <a> that did not have href or title
    – rahmatns
    Mar 24, 2019 at 1:15
  • Why go through all the effort to find a way to execute 2.0 locators when you can convert them to 1.0 compatible locators? Your first locator will do what you want. Can you provide a 2.0 locator that you want to use that can't be converted to an equivalent 1.0?
    – JeffC
    Mar 25, 2019 at 20:33
  • The current version of Chrome is saying that your second XPath isn't valid.
    – JeffC
    Mar 25, 2019 at 20:34
  • @JeffC that is what I mean to fix. The second XPath is a valid XPath version 2.0, but the Chrome is not supporting it. There is a case when given XPath will be always from XPath version 2.0.
    – rahmatns
    Mar 26, 2019 at 1:58

1 Answer 1

2

Here's an xpath 2.0 jquery plugin

after injecting that along with jquery:

for script in ['jquery.js', 'jquery.xpath.js']:
  with open(script, errors='ignore') as f:
    driver.execute_script(f.read())

you should be able to do xpath 2.0 expressions:

driver.execute_script('return $(document).xpath("count(//a)")')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.