Thomas Thomas - 1 month ago 19
Java Question

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:

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]);

Answer

Why are you doing driver.findElement(By.xpath("//*[@class='TweetTextSize js-tweet-text tweet-text']")).getText(); for a second time? Please use the element.getText() instead!