【问题标题】:Selenium: "is not clickable at point" how to wait till all scripts were loadedSelenium:“点不可点击”如何等到所有脚本都被加载
【发布时间】:2017-01-24 07:49:43
【问题描述】:

我有 3 次等待:

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='pagecontrols']/input[1]")));
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='pagecontrols']/input[1]")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='pagecontrols']/input[1]")));

对于同一个元素,但是等待之后,当我想点击时

driver.findElement(By.xpath("//*[@id='pagecontrols']/input[1]")).click();

我有:

“元素(...)在该点不可点击”

我试图通过添加这部分来等待 JavaScript 完全加载

public void waitForLoad(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);
}

但是这不起作用(这在不同的地方有帮助,所以通常在某些情况下会有所帮助),也许我做错了什么? 我想我也应该等待 jQuery,但老实说我知道如何(我已经找到了一些解决方案,但它们对我不起作用)

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element <input type="button" class="btn btn-default" data-toggle="modal" data-target="#adduser" data-localize="adduser" value="Add user"> is not clickable at point (1397, 97). Other element would receive the click: <div class="blockUI blockOverlay" style="z-index: 1000; border: none; margin: 0px; padding: 0px; width: 100%; height: 100%; top: 0px; left: 0px; background-color: rgb(0, 0, 0); cursor: wait; position: absolute; opacity: 0.304712;"></div>
  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 149 milliseconds
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'BTIS1000062581', ip: 'x.x.x.x', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_20'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9), userDataDir=C:\Users\user\AppData\Local\Temp\scoped_dir7068_2467}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=55.0.2883.87, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: e9c9bf2150b10d2865e7a117c1c9e739
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:274)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
    at automationFramework.FirstTestCase.main(FirstTestCase.java:201)

还有一件事:当我添加 线程.sleep(500); 在点击元素之前它正在工作,但我想摆脱那些睡眠

【问题讨论】:

  • 粘贴整个异常。
  • 如果您要点击的元素被另一个元素挡住了,就会发生这种情况。您能否发布您正在测试的网页示例?
  • 此页面只能在我的网络中访问

标签: javascript java jquery selenium webdriver


【解决方案1】:

避免这种“烟雾”UI 阻塞的唯一真正有效的方法是使用 Thread.sleep(); 3000 毫,瞧!

【讨论】:

    【解决方案2】:

    你应该等到拦截点击的元素消失:

    new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath('//div[@class="blockUI blockOverlay"]')));
    driver.findElement(By.xpath("//input[@value='Add user']")).click();
    

    【讨论】:

    • 它工作正常 - 但需要很多时间。老实说我不知道​​发生了什么,它应该每 500 毫秒检查一次这个元素“//div[@class='blockUI blockOverlay']”,最多 10 次,但对我来说它需要超过 5 秒
    • @Krzysztof 然后将 new WebDriverWait(driver, 10) 更改为 new WebDriverWait(driver, 5)
    • 还是一样 - 我试图改变它,但没有结果 System.out.println("before waiting "+System.currentTimeMillis()); new WebDriverWait(driver, 5).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='blockUI blockOverlay']"))); System.out.println("after waiting "+System.currentTimeMillis()); 在等待之前 1485261322976 在等待之后 1485261335940 所以 13s
    【解决方案3】:

    我相信在这里添加等待页面加载应该可以正常工作:

    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    

    【讨论】:

    • 我正在使用这个,但没有结果
    【解决方案4】:

    您应该期望 block-ui 在单击操作或任何其他操作之前消失。 例如:

    public void aguardaBlockUi(){
            WebElement elementBlock = (findElement(By.xpath("//div[@class='blockUI blockOverlay']")));
    
            WebDriverWait wait = new WebDriverWait( driver, 180 );
            wait.until(ExpectedConditions.invisibilityOf(elementBlock ));   
        }
    

    【讨论】:

      【解决方案5】:

      我将 webdriver 子类化为拦截 .find_element_by_*() 并延迟直到所有 blockUI div 都消失了。这使我的主要代码更清晰。

      from selenium.webdriver import Chrome
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.support.ui import WebDriverWait
      
      
      class MyChrome(Chrome):
      
          def find_element(self, by=By.ID, value=None):
              # Wait until jQuery BlockUI Plugin removes of all elements
              # NOTE: driver=super() to avoid RecursionError
              WebDriverWait(super(), 10).until_not(EC.presence_of_element_located(
                  (By.CSS_SELECTOR, 'div.blockUI'),
              ))
      
              return super().find_element(by=by, value=value)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-17
        • 1970-01-01
        • 2020-02-23
        • 2020-02-23
        • 1970-01-01
        • 2020-01-03
        • 1970-01-01
        • 2015-03-22
        相关资源
        最近更新 更多