擅长:python、mysql、java
<p>(一)</p>
<pre><code>from selenium.common.exceptions import NoSuchElementException
def check_exists_by_xpath(xpath):
try:
webdriver.find_element_by_xpath(xpath)
except NoSuchElementException:
return False
return True
</code></pre>
<p>b)<strike>使用xpath-最可靠的。</strike>此外,您可以将xpath作为贯穿所有脚本的标准,并创建上面提到的通用函数。</p>
<p><strong>更新</strong>:我在4年前写下了最初的答案,当时我认为xpath是最好的选择。现在我建议使用css选择器。我仍然建议不要混合/使用“按id”、“按名称”等,而是使用一种单一的方法。</p>