ElementNotVisibleException错误

时间:2015-10-20 07:31:37

标签: java selenium for-loop try-catch

我试图扩展所有评论,回复,在评论中查看更多内容,并在Facebook上发布更多信息。

1)我在下面写的代码允许我扩展我想要的所有内容,除非在帖子中可能有一些回复,即使我已将回复按钮放在循环中。

2)在线程" main"中会出现异常。 org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此如果在所有较小的版本中没有try-catch,则 .click()可能无法与错误进行交互。环路。

    //declare WebDriverWait variable, times out after 20 seconds
    WebDriverWait wait = new WebDriverWait(dr, 20);

    //check element is present on the DOM of a page and visible 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("commentable_item")));
    List<WebElement> comments = dr.findElements(By.className("commentable_item")); //get the comments

    //iterate through the comments 
    for (WebElement comment : comments) {
        boolean clickMore = true;

        try {
            while(clickMore == true) {
                //find web elements by their respective class name
                List<WebElement> commentsbutton = comment.findElements(By.className("UFIPagerLink")); //view more/previous comments
                List<WebElement> repliesbutton = comment.findElements(By.className("UFIPagerIcon")); //replies
                List<WebElement> seemorebutton = comment.findElements(By.className("_5v47")); //see more in comment      
                List<WebElement> seemorelinkbutton = dr.findElements(By.className("see_more_link")); //see more in link 

                //click more comments
                if(commentsbutton.size() > 0) {
                    for (WebElement comments_element : commentsbutton) {
                    //comments_element.click(); //click on button if found
                    //Thread.sleep(5000); //pause for 5 seconds
                        try{                       
                            comments_element.click(); //click on button if found
                            Thread.sleep(5000); //pause for 5 seconds
                        } catch(Exception e){   
                        }                         
                    }

                    for (WebElement replies_element : repliesbutton) {
                    //replies_element.click(); //click on button if found
                    //Thread.sleep(3000); //pause for 3 seconds
                        try{
                            replies_element.click(); //click on button if found
                            Thread.sleep(3000); //pause for 5 seconds
                        } catch(Exception e){   
                        }      
                    }      
                } 
                else clickMore = false; 


                for (WebElement seemorelinks_element : seemorelinkbutton) {
                    try{
                        seemorelinks_element.click(); //click on button if found
                        Thread.sleep(5000); //pause for 5 seconds
                    } catch(Exception e){   
                    }
                }                         

                for (WebElement seemore_element : seemorebutton) {
                    try{
                        seemore_element.click(); //click on button if found
                        Thread.sleep(5000); //pause for 5 seconds
                    } catch(Exception e){   
                    }
                }  
            }
        } catch (NoSuchElementException e) { //when no elements are found
            System.out.println("Comments in this post not found");
        }
    }      
} 

//return the given element if it is visible and has non-zero size, otherwise null.
private static WebElement elementIfVisible(WebElement element) {
  return element.isDisplayed() ? element : null;
}

public static ExpectedCondition<WebElement> visibilityOfElementLocated(final By locator) {
    return new ExpectedCondition<WebElement>() {
        @Override
        public WebElement apply(WebDriver driver) {
            try {
                return elementIfVisible(dr.findElement(locator));
            } catch (StaleElementReferenceException e) {
            return null;
            }
        }
    };
}

}

1 个答案:

答案 0 :(得分:0)

您可以创建一个实用工具方法,并将其放入一些实用工具.java&#39;类如下:

    public void click(WebElement element)
    {
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);
        if(element.isDisplayed())
    element.click();
    }

用法:

WebElement element=driver.findElement(//Locator);
Utility.click(element);

这将确保每次单击元素之前是scrolledIntoView。 如果您需要进一步的帮助,请告诉我。