1

I'm trying to expand all comments, replies, see more in comment, and see more in post in Facebook.

1) The codes I have written below allows me to expand all contents that I want except for maybe a few replies in a post even though I have put the repliesbutton in a loop.

2) There will a Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with error at the .click() if there is no try-catch in all the smaller for loops.

    //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;
            }
        }
    };
}

}

dark_space
  • 179
  • 1
  • 2
  • 17
  • ok..so where is the issue and where are you stuck? – Mrunal Gosar Oct 22 '15 at 06:30
  • @MrunalGosar 1) i dont understand why is there a need for try-catch in for the for loop in order to have no ElementNotVisibleException when im looking for webelement in the page. 2) the codes will sometimes miss out the expanding of a few replies in some posts. is the arrangement for the for loops wrong? – dark_space Oct 23 '15 at 00:39
  • remove the try--catch blocks as there's lot of try catch blocks dangling..handle them in single try--catch..for ur 1st question: after clicking on one reply then the page might get refresh and elements that you've captured would be getting refreshed giving you NoSuchElementException probably. for 2nd question try to scroll to the element before clicking it and 2nd issue would not arise. – Mrunal Gosar Oct 24 '15 at 06:19
  • @MrunalGosar but the try-catch blocks are all in different for loops and there is already a try-catch block that will catch nosuchelementexception containing all the for loops, so how do i handle them in a single try-catch? – dark_space Oct 24 '15 at 16:39
  • @MrunalGosar also,other than from what i have tried and researched on using scrollIntoView, is there a way to scroll to the element before clicking it as you have mentioned? – dark_space Oct 24 '15 at 16:50

1 Answers1

0

You can create a utility method and put it in some 'Utility.java' class something like below:

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

Usage:

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

This will ensure every time before you click the element is scrolledIntoView. Let me know if you need further help on this.

Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71
  • Im sorry, i have tried finding the code you have provided above but was still unable to find it. Or was i supposed to create the 'Utility.java' class on my own? @Mrunal Gosar – dark_space Oct 26 '15 at 01:00
  • @dark_space.. I've edited my post and added scrollIntoView code. Put that any your customUtility class where you maintain all your helper util methods and use it for clicking on element. reference http://stackoverflow.com/questions/3401343/scroll-element-into-view-with-selenium. let me know if you still need help – Mrunal Gosar Oct 29 '15 at 17:40