首页 > 解决方案 > XPath 检查 ID 是否存在于下拉列表中。班级检查

问题描述

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

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

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

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

标签: pythonpython-3.xseleniumclassxpath

解决方案


您可以使用xpathcontains,与find_elementsthat returnsalist和 have a if conditionthat if it is >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

推荐阅读