Selenium on element click,ElementNotInteractableException

时间:2017-08-29 17:51:01

标签: java selenium

我想点击带有href的元素来删除FB组中的待处理帖子,但是selenium会在第二个.click()之后崩溃。使用相同的循环,我可以添加帖子到组没有任何问题; "删除按钮"列表是有效的,因为当我尝试System.out.println("Delete link: " + delete.get(i).getLocation());时,我会获得真实的位置。

while (true){
      if (isloadComplete(driver) && driver.getTitle().contains("My Group")){
         List<WebElement> add = driver.findElements(By.xpath("//a[@data-tooltip-content='approve']"));
         List<WebElement> delete = driver.findElements(By.xpath("//a[@data-tooltip-content='delete']"));
         List<WebElement> articles = driver.findElements(By.xpath("//div[@role='article']"));

         System.out.println("Add links: " + add.size());
         System.out.println("Delete links: " + delete.size());
         System.out.println("Articles: " + articles.size());

         for(int i = 0; i< articles.size(); i++){
             System.out.println("Delete link: " + delete.get(i).getLocation());
             if (articles.get(i).getText().toLowerCase().contains(".com") | articles.get(i).getText().toLowerCase().contains("http")){

                 //System.out.println("Article: " + articles.get(i).getText());

                  delete.get(i).click();
                  WebElement element = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@data-testid='delete_post_confirm_button']")));
                       element.click();

                  }else{
                     add.get(i).click();
                  }

               }
               break;
           }
  

线程中的异常&#34; main&#34; org.openqa.selenium.ElementNotInteractableException:   构建信息:版本:&#39; 3.4.0&#39;,修订版:&#39;未知&#39;,时间:&#39;未知&#39;   系统信息:主持人:&#39; rostyslav-System-Product-Name&#39;,ip:&#39; 127.0.1.1&#39;,os.name:&#39; Linux&#39;,os.arch :&#39; amd64&#39;,os.version:&#39; 4.10.0-32-generic&#39;,java.version:&#39; 1.8.0_131&#39;   驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver   功能[{moz:profile = / tmp / rust_mozprofile.6twl2JYTeRMH,rotate = false,timeouts = {implicit = 0.0,pageLoad = 300000.0,script = 30000.0},pageLoadStrategy = normal,platform = ANY,specificationLevel = 0.0,moz:accessibilityChecks = false,acceptInsecureCerts = false,browserVersion = 55.0.2,platformVersion = 4.10.0-32-generic,moz:processID = 26485.0,browserName = firefox,javascriptEnabled = true,platformName = linux}]   会议ID:562a62e4-3aaf-45e3-b7f2-46f741f798a3       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       at java.lang.reflect.Constructor.newInstance(Constructor.java:423)       at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:150)       在org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)       at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)       在org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)       在org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)       在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)       在org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:272)       在org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:82)       在Main $ HelloRunnable.run(Main.java:67)       在Main.main(Main.java:36)

1 个答案:

答案 0 :(得分:0)

您应该在实际elementToBeClickable来电之前等待click。只需稍微调整以下行,就像之后(通过WebDriverWait):

一样
delete.get(i).click();