Steps involved in: 1)hit the URL: amazon.in 2)search for "Sony Bluetooth headphone" 3)on the search result page you will found "1-16 of 152 results for "Sony Bluetooth headphone" below the navigation bar. 4) Using Pagination we have to go each and every page and print the all the 152 products names
I trying it many ways but which is not working properly can you please suggest any modification in my program
Here is my code.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\selenium\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://amazon.in");
driver.manage().window().maximize();
String searchKeyword= "sony bluetooth headphone";
driver.findElement(By.id("twotabsearchtextbox")).sendKeys(searchKeyword);
driver.findElement(By.id("nav-search-submit-button")).click();
String searchResult=driver.findElement(By.xpath("//span[@class=\"a-color-state a-text-bold\"]")).getText().replaceAll("\"", "");
System.out.println(searchResult);
if(searchKeyword.contains((searchResult))) {
System.out.println("Search function working fine");
}
else
System.out.println("search function not working fine");
int paginationSize=Integer.parseInt((driver.findElement(By.xpath("(//li[@class=\"a-disabled\"])[2]")).getText()));
System.out.println(paginationSize);
List<String> allelements=new ArrayList<>();
for(int i=0;i<paginationSize;i++) {
Thread.sleep(4000);
driver.findElement(By.xpath("//li[@class=\"a-last\"]")).click();
List<WebElement> Names=driver.findElements(By.xpath("//span[@class=\"a-size-medium a-color-base a-text-normal\"]"));
for(int i1=0;i1<Names.size();i1++) {
allelements.add(Names.get(i1).getText());
}
}
System.out.println(allelements.size());
for(String element:allelements) {
System.out.println(allelements);
System.out.println("----------------------------------------------");
}
}}