2015-10-19 104 views
2

我知道有关于这个主题的几个主题,但我不一定在寻找解决方案,而是一个解释。我在一个非常大的自动化套件上工作,该套件使用浏览器通过手机测试Web应用程序。我的稳定性很低..这是由于这个错误引发了我!偶尔它会工作,偶尔它不会..我不能使用操作,因为Browserstack不支持该..为什么这个错误存在,并有任何人有任何成功解决它。我总是在等待一个使用wait.until(ExpectedConditions)的对象,但有时候这样做效果不好。因为它是一个未知的错误,所以我不能把它作为例外来捕捉。另外,我们的标准不允许使用Thread.sleep()。有任何想法吗?谢谢你这么多Selenium WebDriver - 元素不可点击,为什么?

enter image description here

这里是一些代码的屏幕.. enter image description here

+0

你可以张贴一些故障跟踪我们调用这个函数 public static void waitForElementPresent(final By by, int timeout,WebDriver driver) 在此之后?或者任何代码? – jagdpanzer

+0

这也可能是一个有争议的问题,但是你需要使用BrowserStack吗? – jagdpanzer

+0

@jagdpanzer,我更新了这篇文章给你一个例子。目前这是我们拥有的最佳解决方案。我们现在正在使用硒电池。 – Tree55Topz

回答

1

元素通常不能够点击,由于以下原因。

  1. HTML是加载和客户端仍然从服务器
  2. 由于一些对象重复的对象

问题3接收更新滚动时

  • 它可以是解决不了的,你需要修正你的代码在这等待我等待HTML准备,然后验证它是否可以点击或不能这已经消除了从我的代码这样的例外

    我怎么做了解决方案的概率lem 1和2,您可以简单地使用我的自定义等待,然后单击。如果你正在使用其他的浏览器之后,Chrome,然后调用滚动到对象,这将解决你的问题 代码

    public static void waitForElementPresent(final By by, int timeout,WebDriver driver) { 
    
         waitForPageLoad(driver); 
         WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,40).ignoring(StaleElementReferenceException.class); 
         /* wait.until(new ExpectedCondition<Boolean>(){ 
          @Override 
          public Boolean apply(WebDriver webDriver) { 
           WebElement element = webDriver.findElement(by); 
           return element != null && element.isDisplayed(); 
          } 
          }); */ 
          try { 
          Thread.sleep(1000); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
          wait.until(ExpectedConditions.presenceOfElementLocated(by)); 
          wait.until(ExpectedConditions.elementToBeClickable(by)); 
          WebDriverWait wait2 = new WebDriverWait(driver, 40); 
          wait2.until(ExpectedConditions.elementToBeClickable(by)); 
    
         } 
        //wait for page to laod 
        public static void waitForPageLoad(WebDriver driver) { 
         ExpectedCondition<Boolean> pageLoadCondition = new 
          ExpectedCondition<Boolean>() { 
           public Boolean apply(WebDriver driver) { 
            return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete"); 
           } 
          }; 
         WebDriverWait wait = new WebDriverWait(driver, 30); 
         wait.until(pageLoadCondition); 
        } 
    
  • 0

    这是由于的速度,硒运行。它会在页面加载之前尝试查找元素,从而导致出现此错误。

    2

    您正在等待WebElement可点击,然后再次查找WebElements列表并单击第一个元素。 这并不能保证你点击你等待点击的元素。

    public void waitAndClickElement(WebElement element) { 
        driverWait.until(ExpectedConditions.elementToBeClickable(element)).click(); 
    } 
    

    在你的情况,

    public void clickImageView() { 
        driverWait.until(ExpectedConditions.elementToBeClickable(listImageView)).click() ; 
    } 
    
    +0

    我会尝试这一点,因为它似乎使很多感觉!谢谢! – Tree55Topz

    0

    对于您所提供的代码示例中,.until()返回WebElement你正在等待。你可以使用下面的代码来点击它,而不是再次抓取页面。

    public void clickImageView() 
    { 
        driverWait.until(ExpectedConditions.elementToBeClickable(listImageView)).click(); 
    } 
    
    相关问题