4

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.

rahmatns
  • 147
  • 1
  • 13
  • This will select the attributes not the elements. Is that what you intended? – pguardiario Mar 24 '19 at 01:01
  • @pguardiario I mean to select the elements that contains those attribute. so it will not retrieve `` that did not have `href` or `title` – rahmatns Mar 24 '19 at 01: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 '19 at 20:33
  • The current version of Chrome is saying that your second XPath isn't valid. – JeffC Mar 25 '19 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 '19 at 01:58

1 Answers1

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)")')
pguardiario
  • 51,516
  • 17
  • 106
  • 147