python - 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)
解决方案
您可以使用xpath
contains,与find_elements
that returns
alist
和 have a if condition
that 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
推荐阅读
- python - 如何实时增加或减少声音的音量(pydub)?
- ios - 反应本机 ios “本机模块不能为空”
- list-comprehension - 在 manim ReplacementTransform 动画中使用 *args
- python - Sparkessions Pyspark 的最佳实践
- android - 在 ConstraintLayout 中放置自定义视图
- typescript - TypeScript:如果编译器对可达性很聪明,如何抛出错误?
- mysql - 我找不到我的 MySql 查询的问题?
- javascript - 违反'setTimeout'处理程序采取
小姐 - abap - 在没有循环的情况下获得总数
- javascript - 如何通过比较两个数组中的 id 将第一个数组中的值分配给第二个数组中的新键。在javascript中