我試圖展開所有評論,回覆,看到更多的評論,看到更多的在崗的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;
}
}
};
}
}
ok..so哪裏是問題,你在哪裏卡住了? –
@MrunalGosar 1)我不明白爲什麼有需要的try-catch中的用於以無ElementNotVisibleException循環時,即時尋找在頁面webelement。 2)代碼有時會錯過某些帖子中擴大的幾個回覆。 for循環的安排是否錯誤? –
刪除try-catch塊,因爲有大量的try catch塊懸空..處理它們在單一的try - catch..for你的第一個問題:點擊一個回覆後,頁面可能會得到刷新和元素,你已經捕獲將得到刷新給你可能NoSuchElementException。對於第二個問題,嘗試在單擊之前滾動到元素,第二個問題不會出現。 –