【问题标题】:XPath Check if ID exists within drop down. Class checkXPath 检查 ID 是否存在于下拉列表中。班级检查
【发布时间】:2021-10-27 13:42:30
【问题描述】:

提前致谢。 Noobie to Python here,我正在尝试通过 Selenium 和相应的 XPath 值自动将值输入到网站中。

它的功能应该是我将动态“ID”的键发送到输入框中,然后会弹出 ID 并选择 ID。这行得通。现在我遇到了 ID 不存在并且该工具最终停止运行的问题。我知道我需要一个 If 函数,然后执行一个 elif 如果它不存在,但是当涉及到这些带有 XPaths 的语句时我迷失了,需要一点指导。

我有声明 ID 不存在的弹出值的类 XPath:

<li class="vv_list_no_items vv_item_indent listNoItems">No results match "1234567890123456"</li>

令人困惑的部分还有动态 ID,其中“1234567890123456”可以是任何 ID。

当前代码如下,抱歉缩进,因为这是从一大堆脚本中提取的。

                try:
                   wait = WebDriverWait(browser, 10)
                   # Inputs Legal Entity
                   elem = wait.until(EC.element_to_be_clickable((By.XPATH,
                                                                 "//*[@id='di3Form']/div[2]/div[2]/div/div[1]/div[3]/div/div[2]/div/div[1]/input"))).send_keys(
                       LE)
                   elem = wait.until(
                       EC.element_to_be_clickable((By.XPATH, "//*[@id='veevaBasePage']/ul[3]/li/a"))).click()

                   LE = None

                   # Inputs WWID

                   elem = wait.until(EC.element_to_be_clickable((By.XPATH,
                                                                 "//*[@id='di3Form']/div[2]/div[2]/div/div[1]/div[4]/div/div[2]/div/div[1]/input"))).send_keys(ID)
                   
                   elem = wait.until(
                       EC.element_to_be_clickable((By.XPATH, "//*[@id='veevaBasePage']/ul[4]/li[2]/a/em"))).click()

                   # Inputs Country
                   elem = wait.until(EC.element_to_be_clickable((By.XPATH,
                                                                 "//*[@id='di3Form']/div[2]/div[2]/div/div[1]/div[5]/div/div[2]/div/div[1]/input"))).send_keys(
                       Country)
                   elem = wait.until(
                       EC.element_to_be_clickable((By.XPATH, "//*[@id='veevaBasePage']/ul[5]/li/a"))).click()

                   # Save
                   elem = wait.until(EC.element_to_be_clickable((By.XPATH,
                                                                 "//a[@class='docInfoSaveButton save vv_button vv_primary']/span[@class='vv_button_text vv_ellipsis' and text()='Save']")))
                   browser.execute_script("arguments[0].click();", elem)


                   wait = WebDriverWait(browser, 15)

                   # Click dropdown menu arrow
                   elem = wait.until(EC.element_to_be_clickable(
                       (By.XPATH, "//*[@id='di3Header']/div[3]/div/div[2]/div[1]/div/div[2]/div/div/button")))
                   browser.execute_script("arguments[0].click();", elem)

                   wait = WebDriverWait(browser, 100)

                   # Click "Publish"
                   elem = wait.until(EC.element_to_be_clickable((By.XPATH,
                                                                 "/html/body/div[6]/div/ul/li")))
                   browser.execute_script("arguments[0].click();", elem)

                   #Confirm Publish
                   elem = wait.until(EC.element_to_be_clickable((By.XPATH,
                                                                 "//a[@class='save vv_button vv_primary']/span[@class='vv_button_text' and text()='Yes']")))
                   browser.execute_script("arguments[0].click();", elem)

【问题讨论】:

    标签: python python-3.x selenium class xpath


    【解决方案1】:

    您可以使用 xpath 包含,find_elements 表示 returnslist 并有一个 if condition 如果它是 is &gt;0,那么 No match found 字符串将出现在 UI 中。

    try :
        no_match = "No results match" + " " + '"' + WWID + '"'
    
        if (len(driver.find_elements(By.XPATH, "//li[contains(text(),'{}')]".format(no_match)))) > 0:
            print("Code has found No results match, String ")
            # do what ever you wanna do here. 
        else:
            print("There is not locator that contains, No results match")
    except:
        print("Something went wrong")
        pass
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      相关资源
      最近更新 更多