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.

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
  • 53,827
  • 19
  • 119
  • 159