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?