0

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("----------------------------------------------");
     }


}}
Bhargav
  • 21
  • 4
  • "some exceptions like" is not what we need. We need exact stack traces. – J Fabian Meier Jan 06 '21 at 15:11
  • Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//li[@class="a-last"]"} (Session info: chrome=87.0.4280.88) unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true} Session ID: 2aa6571022add805d0da33c661b65e91 *** Element info: {Using=xpath, value=//li[@class="a-last"]} – Bhargav Jan 06 '21 at 15:25
  • Please format the post correctly...otherwise it's really hard to read... – khmarbaise Jan 06 '21 at 15:26
  • Don't you have a stack trace? – J Fabian Meier Jan 06 '21 at 15:40
  • yes, I have but its huge so I didn't paste it earlier. – Bhargav Jan 06 '21 at 16:06

0 Answers0