Python selenium presence_of_element_located((By.XPATH, ) need case insensitive

12,765

Solution 1

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")

Solution 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")]'
Share:
12,765

Related videos on Youtube

Alexei Martianov
Author by

Alexei Martianov

Updated on June 04, 2022

Comments

  • Alexei Martianov
    Alexei Martianov almost 2 years

    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/")]')))
    
  • JeffC
    JeffC almost 8 years
    If you answered your own question, please mark this as the answer.