用于检查元素是否存在的Python Selenium Webdriver需要时间

Abh*_*rni 6 python selenium xpath selenium-webdriver

尝试在几次GUI操作后验证某些按钮不存在(预计不存在).我使用的是find_element_by_xpath(),但速度非常慢.超时的任何解决方案?

San*_*rma 8

实际上,如果找不到指定的元素,WebDriver的find_element方法将等待元素的隐式时间.

WebDriver中没有像isElementPresent()那样的预定义方法来检查.你应该为此编写自己的逻辑.

逻辑

public boolean isElementPresent()
{
   try
   {
      set_the_implicit time to zero
      find_element_by_xpath()
      set_the_implicit time to your default time (say 30 sec)
      return true;
   }
   catch(Exception e)
   {
       return false;
   }
}
Run Code Online (Sandbox Code Playgroud)

见:http://goo.gl/6PLBw