3

I stumbled upon fact then xref maybe in upper case or lower case (even partly) on searched page. How to ensure script finds xref if xref is present on page regardless of case?

Post Python: Selenium xpath to find element with case insensitive characters? talks it's not working, but it was 2013. Hope it's changed. How to write proper expression? My current code is:

myLink = WebDriverWait(myDriver, 
10).until(ExConditions.presence_of_element_located((By.XPATH, 
'//a[starts-with(@href,"https://www.test.org/portal/")]')))
Community
  • 1
  • 1
Alex Martian
  • 3,423
  • 7
  • 36
  • 71

2 Answers2

2

Looks like lower-case(@href) is not working (at least for my Firefox on Windows 7), also not fn:lower-case(@href).

I resort to translate which I've found in posts:

'//a[contains(translate(@href, "ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz"),
"www.test.org/portal")]'
Alex Martian
  • 3,423
  • 7
  • 36
  • 71
2

Try this function.

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

def If_element_is_here_do_click(xpath_arg):
        executed = 0
        while (executed == 0):
            try:
                driver.find_element(By.XPATH, xpath_arg).click()
                executed = 1
                print("Clicked")
            except:
                print("No")
                time.sleep(0.5)

And call like this :

If_element_is_here_do_click("/html/body/div[1]/section/main/div/div/article/div/div[2]/div/div[2]/section[1]/span[1]/button")
Liam-Nothing
  • 167
  • 8