【问题标题】:Python selenium presence_of_element_located((By.XPATH, ) need case insensitivePython selenium presence_of_element_located((By.XPATH, ) 需要不区分大小写
【发布时间】:2016-06-19 20:24:10
【问题描述】:

我偶然发现了事实,然后xref 在搜索页面上可能是大写或小写(甚至部分)。如果页面上存在外部参照,如何确保脚本找到外部参照?

发帖Python: Selenium xpath to find element with case insensitive characters? 说它不起作用,但那是 2013 年。希望它有所改变。如何写出正确的表达方式?我当前的代码是:

myLink = WebDriverWait(myDriver, 
10).until(ExConditions.presence_of_element_located((By.XPATH, 
'//a[starts-with(@href,"https://www.test.org/portal/")]')))

【问题讨论】:

    标签: python selenium xpath case-sensitive


    【解决方案1】:

    看起来lower-case(@href) 不起作用(至少对于我在 Windows 7 上的 Firefox),fn:lower-case(@href) 也不起作用。

    我求助于我在帖子中找到的translate

    '//a[contains(translate(@href, "ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz"),
    "www.test.org/portal")]'
    

    【讨论】:

    • 如果您回答了自己的问题,请将其标记为答案。
    【解决方案2】:

    试试这个功能。

    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)
    

    然后这样调用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 2012-12-01
      • 2013-03-06
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多