Python 如何使用if-elif-else检查元素是否存在
我有一个下拉菜单,我需要检查它的条目。如果没有条目,我可以添加新条目,如果只有一个条目,我将其删除并添加新条目,当我有多个条目(>=2)时,我无法继续添加条目。我可以通过Python 如何使用if-elif-else检查元素是否存在,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我有一个下拉菜单,我需要检查它的条目。如果没有条目,我可以添加新条目,如果只有一个条目,我将其删除并添加新条目,当我有多个条目(>=2)时,我无法继续添加条目。我可以通过person\u rem\u btn进行检查。如果我只有一个按钮doc\u person\u table:0:person\u rem\u btn我可以继续,如果我有第二个按钮doc\u person\u table:1:person\u rem\u btn我无法继续 我得到一个例外: selenium.common.excep
person\u rem\u btn
进行检查。如果我只有一个按钮doc\u person\u table:0:person\u rem\u btn
我可以继续,如果我有第二个按钮doc\u person\u table:1:person\u rem\u btn
我无法继续
我得到一个例外:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='frmMain:doc_person_table:1:person_rem_btn']/span[1]"}
然而,这就是重点,这个元素可能不可用,我只是检查它的存在。我将感谢任何帮助。这是我的密码:
if driver.find_element_by_xpath("//*[@id='frmMain:doc_person_table:1:person_rem_btn']/span[1]") == True:
print ("there are already many entries")
driver.close()
elif (driver.find_element_by_xpath("//*[@id='frmMain:doc_person_table:1:person_rem_btn']/span[1]") == False and driver.find_element_by_xpath("//*[@id='frmMain:doc_person_table:0:person_rem_btn']/span[1]") == True):
print ("there is only one entry, it will be removed to proceed")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='frmMain:doc_person_table:0:person_rem_btn']/ span[1]"))).click()
else:
print ("there is no entry, you can proceed")
find_element\u by_xpath
不返回True
或False
,它返回WebElement
或抛出NoSuchElementException
。您可以使用find_elements\u by_xpath
获取列表并检查此列表是否包含任何元素。从等待一个不相关的元素开始,该元素可以指示页面已加载
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[@id^='frmMain:doc_person_table")))
# assuming this element will always appear when the page is loaded
elements = driver.find_elements_by_xpath("//*[@id='frmMain:doc_person_table:1:person_rem_btn']/span[1]")
if elements: # more verbose if len(elements) > 0
print ("there are already many entries")
driver.close()
else:
elements = driver.find_elements_by_xpath("//*[@id='frmMain:doc_person_table:0:person_rem_btn']/span[1]")
if elements:
print ("there is only one entry, it will be removed to proceed")
elements[0].click()
else:
print ("there is no entry, you can proceed")
您可以采用几种方法,其中一种方法是创建一个下拉项列表,用于显示所有元素的可见性(),并探测列表大小:
- 使用
:CSS\u选择器
list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "[id*='doc_person_table'][id$='person_rem_btn']>span"))) if not list: print ("there is no entry, you can proceed") # other steps elif len(list) == 1 print ("there is only one entry, it will be removed to proceed") # other steps else: print ("there are already many entries") break
list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[contains(@id, 'doc_person_table') and contains(@id, 'person_rem_btn')]/span"))) if not list: print ("there is no entry, you can proceed") # other steps elif len(list) == 1 print ("there is only one entry, it will be removed to proceed") # other steps else: print ("there are already many entries") break
- 使用
:XPATH
list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "[id*='doc_person_table'][id$='person_rem_btn']>span"))) if not list: print ("there is no entry, you can proceed") # other steps elif len(list) == 1 print ("there is only one entry, it will be removed to proceed") # other steps else: print ("there are already many entries") break
list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[contains(@id, 'doc_person_table') and contains(@id, 'person_rem_btn')]/span"))) if not list: print ("there is no entry, you can proceed") # other steps elif len(list) == 1 print ("there is only one entry, it will be removed to proceed") # other steps else: print ("there are already many entries") break
find_elements\u by_xpath
,请参阅更新的答案。@jitlp您可以为不相关的元素添加WebDriverWait
,您知道这些元素将始终存在,并指示页面已加载。请参阅更新的答案。@jitlp此元素只是一个示例,您可以选择任何其他元素。它只是用来表示页面已加载。我不熟悉这个网站,所以我根据问题中的定位器发布了。@jitlp您是指elements=driver中的元素。通过xpath(“/*[@id='frmMain:doc\u person\u table:0:person\u rem\u btn']/span[1]”和元素[0]查找元素。单击()?或者其他与此无关的问题?@jitlp这听起来像是一个新问题,如果没有看到更详细的代码和html,在评论中很难回答,但您应该记住,移动到另一个页面或刷新页面(click()
有时会导致页面刷新)使以前定位的所有图元无效,您需要重新定位它们。