擅长:python、mysql、java
<p><code>find_element_by_xpath</code>不返回<code>True</code>或<code>False</code>,它返回<code>WebElement</code>或抛出<code>NoSuchElementException</code>。您可以使用<code>find_elements_by_xpath</code>获取列表并检查此列表是否包含任何元素。从等待一个不相关的元素开始,该元素可以指示页面已加载</p>
<pre><code>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")
</code></pre>