2015-10-20 80 views
1

我試圖展開所有評論,回覆,看到更多的評論,看到更多的在崗的Facebook。ElementNotVisibleException錯誤

1)我已經寫在下面的代碼可以讓我展開我想除了在即使我已經把repliesbutton在一個循環後也許一些回覆的所有內容。

2)將在線程異常「主」 org.openqa.selenium.ElementNotVisibleException:元素是不可見的,所以可能無法與錯誤在。點擊()互動如果沒有嘗試捕捉所有較小的循環。

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

}

+0

ok..so哪裏是問題,你在哪裏卡住了? –

+0

@MrunalGosar 1)我不明白爲什麼有需要的try-catch中的用於以無ElementNotVisibleException循環時,即時尋找在頁面webelement。 2)代碼有時會錯過某些帖子中擴大的幾個回覆。 for循環的安排是否錯誤? –

+0

刪除try-catch塊,因爲有大量的try catch塊懸空..處理它們在單一的try - catch..for你的第一個問題:點擊一個回覆後,頁面可能會得到刷新和元素,你已經捕獲將得到刷新給你可能NoSuchElementException。對於第二個問題,嘗試在單擊之前滾動到元素,第二個問題不會出現。 –

回答

0

您可以創建一個實用的方法,並把它放在一些 'Utility.java' 級類似如下:

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。 讓我知道你是否需要進一步的幫助。

+0

我很抱歉,我試圖找到上面提供的代碼,但仍無法找到它。還是我應該自己創建'Utility.java'類? @Mrunal Gosar –

+0

@dark_space ..我編輯了我的帖子並添加了scrollIntoView代碼。把所有你的customUtility類放在你維護所有你的helper util方法的地方,並用它來點擊元素。參考http://stackoverflow.com/questions/3401343/scroll-element-into-view-with-selenium。讓我知道你是否仍然需要幫助 –

相關問題