0

What is the correct way to call the tokenize function in a By.XPATH selector in selenium. Here is a snippet of what seems correct but this errors with "The string 'tokenize(//font[@face='sans-serif' and @size='4'], "*")[2]' is not a valid XPath expression".

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# //font[@face='sans-serif' and @size='4'] returns:
# "Graphics * Unix * Networks * Fun"
# select 3rd token using "*"
xpath: str = "tokenize(//font[@face='sans-serif' and @size='4'], \"*\")[2]"
with webdriver.Edge() as driver:
    try:     
        driver.get("http://acme.com")
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, xpath))
        )
        print(element.text)
    except Exception as e:
        print(f" Failed to find element due to: {e}") 
    finally:
        driver.quit()

What is a xpath only method to tokenize on a character (or string) and select the nth item? Is there selenium specific syntaxt?

  • I have been blocked from posting future questions due to this question, how is this out of scope of StackOverflow.com? –  Jul 11 '23 at 01:05

1 Answers1

1

The function fn:tokenize is part of XPath-2.0. But selenium only supports XPath-1.0 in the default configuration.

The answers in this SO question claim that there may be plugins to realize XPath-2.0 support. But I haven't checked their validity.

What is a xpath only method to tokenize on a character (or string) and select the nth item?

I don't know of any XPath-1.0 functions that can achieve that. Sorry.
The closest may be using a fn:translate(inputString,'char','
') which splits the string by each "char" creating a newline between each substring. This only works for chars and not strings.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • Thanks for the answer, I had read that too and thought maybe some 2.0 functions worked. –  Jun 28 '23 at 21:46