简体   繁体   English

使用Java / Selenium创建tweets的字符串数组

[英]Using Java/Selenium to create a String Array of tweets

I am trying to create a string array of tweets made up of all the tweets on a particular page. 我正在尝试创建由特定页面上所有推文组成的推文字符串数组。 I have loaded to the page I want and can add the first tweet on that page into the string array and print it. 我已经加载到所需的页面,并且可以将该页面上的第一条推特添加到字符串数组中并进行打印。 But for some reason, the first tweet gets added over and over rather than getting the first tweet, moving to the second, then the third, etc. Here is my code: 但是由于某种原因,第一个tweet会一遍又一遍地添加,而不是获得第一个tweet,而是移到第二个,然后是第三个,依此类推。这是我的代码:

String[] tweets = new String[50];
int count=0;

List<WebElement> elements = driver.findElements(By.xpath("//*[@class='TweetTextSize  js-tweet-text tweet-text']"));
for (WebElement element : elements) {
    if (element.isDisplayed()) {
        tweets[count] = driver.findElement(By.xpath("//*[@class='TweetTextSize  js-tweet-text tweet-text']")).getText();
        count++;
    }
}

for(int count2=0; count2<tweets.length; count2++)
    System.out.println(tweets[count2]);

Why are you doing driver.findElement(By.xpath("//*[@class='TweetTextSize js-tweet-text tweet-text']")).getText(); 你为什么要做driver.findElement(By.xpath("//*[@class='TweetTextSize js-tweet-text tweet-text']")).getText(); for a second time? 第二次? Please use the element.getText() instead! 请改用element.getText()

This line does not change depending on your loop variable, so you get the same thing back every time: 该行不会更改,具体取决于您的循环变量,因此您每次都会得到相同的结果:

        tweets[count] = driver.findElement(By.xpath("//*[@class='TweetTextSize  js-tweet-text tweet-text']")).getText();

You probably want: 您可能想要:

        tweets[count] = element.getText();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM