I am using css selector ,currently i want to print all the news text which is present on carousels in the form of set of 5 news
I have tried multiple combinations but unable to print all the news
Currently with help li[class*='Carousel-Item Grid-U-1-5'] path I am able to print on only first set of five news
Once i click on navigation button for next set of 5 news am unable to print it
How should I solve this problem
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class yahoo {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://in.yahoo.com");
Thread.sleep(1000);
driver.findElement(By.xpath("//button[@class='js-today-playpause PlayPauseBtn ButtonNaked']")).click();
List<WebElement> news = driver.findElements(By.cssSelector("li[class*='Carousel-Item Grid-U-1-5']"));
int newnews=news.size();
System.out.println(newnews);
for(int i=5;i<news.size();i=+5)
{
for(int j=0;j<i;j++)
{
System.out.println(news.get(j).getText());
}
Thread.sleep(7000);
driver.findElement(By.xpath("//button[@title='Next'] ")).click();
}
// }
//
}
}
there you go
[quote:3cehy1wt]package com.soapuitutorial.propertie;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class yahoo {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://in.yahoo.com");
Thread.sleep(1000);
driver.findElement(
By.xpath("//button[@class='js-today-playpause PlayPauseBtn ButtonNaked']"))
.click();
List<WebElement> news = driver.findElements(By
.cssSelector("li[class*='Carousel-Item Grid-U-1-5']"));
int newnews = news.size();
System.out.println(newnews);
for (int j = 0; j < news.size(); j++) {
System.out.println(news.get(j).getText());
}
Thread.sleep(7000);
driver.findElement(By.xpath("//button[@title='Next'] ")).click();
news = driver.findElements(By
.cssSelector("li[class*='Carousel-Item Grid-U-1-5']"));
newnews = news.size();
System.out.println(newnews);
for (int j = 0; j < news.size(); j++) {
System.out.println(news.get(j).getText());
}
driver.findElement(By.xpath("//button[@title='Next'] ")).click();
news = driver.findElements(By
.cssSelector("li[class*='Carousel-Item Grid-U-1-5']"));
newnews = news.size();
System.out.println(newnews);
for (int j = 0; j < news.size(); j++) {
System.out.println(news.get(j).getText());
}
}
}[/quote:3cehy1wt]
Thanks for the help
Is there any way to optimize this logic
Because currently there is individual loop for each set of news (ex.if there are 55 news so i have to write 11 for loops)
can you please help to provide optimum solution
I'm tied up with my other project, so i can't help you, sorry. I'm sure you can find optimum solution. basically you have to put it in a loop.