gpt4 book ai didi

java - 尽管不再有 WebElement,循环 List 仍不会停止

转载 作者:行者123 更新时间:2023-12-01 10:51:15 26 4
gpt4 key购买 nike

我想在 YouTube 评论页面上按此顺序单击这三个不同的网络元素 showmorebutton、viewrepliesbutton、readmorebutton(例如 https://www.youtube.com/watch?v=VtTRcWXSBwc )。

下面是我编写的代码。

//iterate through the comments 
//for(int j=0; j <= commententry.size(); j++){
for (WebElement comment : commententry) {
boolean clickMore = true;

try {
while(clickMore == true) {
//int counting = 0;
//if (counting == j){
//find web elements by class name
List<WebElement> viewrepliesbutton = comment.findElements(By.xpath("//a[@class='show-more']//span[@class='sprite_caret down']")); //view replies
List<WebElement> readmorebutton = comment.findElements(By.className("comment-text-toggle")); //read more
List<WebElement> showmorebutton = dr.findElements(By.xpath("//div[contains(@id,'yt-comments-paginator')]")); //show more

try{
//click more comments
if(showmorebutton.size() > 0) {
for (WebElement showmore_element : showmorebutton ) {
Utility.click(showmore_element); //click on button if found
System.out.println("show more ");
Thread.sleep(3000); //pause for 5 seconds
}
} else clickMore = false;

for (WebElement viewreplies_element : viewrepliesbutton ) {
Utility.click(viewreplies_element); //click on button if found
System.out.println("view replies");
Thread.sleep(3000); //pause for 5 seconds
}

for (WebElement readmore_element : readmorebutton){
Utility.click(readmore_element); //click on button if found
System.out.println("read more");
Thread.sleep(3000); //pause for 5 seconds
}

//j++;
} catch(Exception e){
Logger.getLogger(ExpandYoutube.class.getName()).log(Level.SEVERE, "Oops, something happened!", e);
}
//} else break;
}
} catch (NoSuchElementException e) { //when no elements are found
Logger.getLogger(ExpandYoutube.class.getName()).log(Level.SEVERE, "Oops, something happened!", e);
}
}

问题:即使页面上不再有 WebElement,循环仍然继续,已添加 else break;else clickMore = false; code> 停止循环但失败。

输出在这种情况下,应该在循环的第二次停止,但继续进行第三次和第四次循环。

    run:
.
Page Loaded.
Pausing for 5 seconds: 1 2 3 4 5
[[[FirefoxDriver: firefox on WINDOWS (279f52ea-c071-45c9-96d4-6339a22d5a51)] -> xpath: //div[@class='comment-replies-header']], [[FirefoxDriver: firefox on WINDOWS (279f52ea-c071-45c9-96d4-6339a22d5a51)] -> xpath: //div[@class='comment-replies-header']], [[FirefoxDriver: firefox on WINDOWS (279f52ea-c071-45c9-96d4-6339a22d5a51)] -> xpath: //div[@class='comment-replies-header']], [[FirefoxDriver: firefox on WINDOWS (279f52ea-c071-45c9-96d4-6339a22d5a51)] -> xpath: //div[@class='comment-replies-header']], [[FirefoxDriver: firefox on WINDOWS (279f52ea-c071-45c9-96d4-6339a22d5a51)] -> xpath: //div[@class='comment-replies-header']], [[FirefoxDriver: firefox on WINDOWS (279f52ea-c071-45c9-96d4-6339a22d5a51)] -> xpath: //div[@class='comment-replies-header']]]
show more
view replies
view replies
view replies
view replies
view replies
view replies
show more
view replies
view replies
view replies
view replies
view replies
view replies
view replies
view replies
view replies
view replies
show more
view replies
view replies
view replies
view replies
view replies
view replies
view replies
view replies
BUILD STOPPED (total time: 4 minutes 12 seconds)

最佳答案

您应该进行迭代,直到 showmorebutton 可见,在循环内单击 readmorebuttonviewrepliesbutton。请参阅下面的代码来给出一个想法:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("yt-comments-list")));

try {
while (dr.findElements(By.xpath("//div[@id='yt-comments-paginator']")).size() > 0) {
System.out.println("show more ");
dr.findElement(By.xpath("//div[@id='yt-comments-paginator']")).click();
Thread.sleep(3000); //pause for 5 seconds
}
} catch(Exception e){
Logger.getLogger(ExpandYoutube.class.getName()).log(Level.SEVERE, "Oops, something happened!", e);
}

List<WebElement> readmorebutton = dr.findElements(By.className("comment-text-toggle"));
List<WebElement> viewrepliesbutton = dr.findElements(By.xpath("//a[@class='show-more']//span[@class='sprite_caret down']"));

for (WebElement viewreplies_element : viewrepliesbutton ) {
Utility.click(viewreplies_element); //click on button if found
System.out.println("view replies");
Thread.sleep(3000); //pause for 5 seconds
}

for (WebElement readmore_element : readmorebutton){
Utility.click(readmore_element); //click on button if found
System.out.println("read more");
Thread.sleep(3000); //pause for 5 seconds
}

关于java - 尽管不再有 WebElement,循环 List<WebElement> 仍不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33884866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com