Home > Software design >  Trying to click 2 checkboxes but not able to do so (Selenium Java)
Trying to click 2 checkboxes but not able to do so (Selenium Java)

Time:08-22

Site : https://www.myntra.com/men-tshirts

`WebElement brand = driver.findElement(By.xpath("//div[@class='brand-more']"));
        brand.click();
        
        WebElement select_brand = driver.findElement(By.xpath("//input[@placeholder='Search brand']"));
        select_brand.sendKeys("H");
        
        List<WebElement> select_HRX = driver.findElements(By.xpath("//label[@class=' common-customCheckbox']"));
        
        for(int i=0; i<select_HRX.size(); i  ) {
            String brandText = select_HRX.get(i).getText();
//          String[] arr = brandText.split("\\(");
            String[] arr1 = brandText.split(Pattern.quote("("));
            System.out.println(arr1[0]);
            if(arr1[0].equalsIgnoreCase("HRX by Hrithik Roshan") && arr1[0].equalsIgnoreCase("HERE&NOW")) {
                 select_HRX.get(i).click();
                 Thread.sleep(2000);
                 break;
            }
        }`

// trying to click 2 checkboxes based on condition not able to do any suggestions ??

CodePudding user response:

Here

arr1[0].equalsIgnoreCase("HRX by Hrithik Roshan") && arr1[0].equalsIgnoreCase("HERE&NOW")

You should change from && to || i.e. from logical AND to logical OR operator since each of 2 desired elements you want to click will contain first OR second text, not BOTH of them.
In case you know the anount of checkboxes you want to select you can add counter, like the below:

counter = 0;
if(arr1[0].equalsIgnoreCase("HRX by Hrithik Roshan") || arr1[0].equalsIgnoreCase("HERE&NOW")) {
    counter  ;
    select_HRX.get(i).click();
    Thread.sleep(1000);
    if(counter>1){
        break;
    }
}

CodePudding user response:

There's a better, easier, and faster way to do all of this without loops, etc. You need to build your locators using the brand names you are looking for instead of grabbing all elements and then looping to look for a match. It's also a good practice to wait for elements to be in the correct state before interacting with them to avoid unexpected exceptions, etc.

The basic flow is...

  1. Navigate to the page
  2. Click the Brand search icon
  3. Enter Brand #1
  4. Click the checkbox next to Brand #1
  5. Wait for Brand #1 to show up as filtered on the page (this is important)
  6. Clear the Brand search box
  7. Enter Brand #2
  8. Click the checkbox next to Brand #2
  9. Wait for Brand #2 to show up as filtered on the page (this is important)

Now both of the desired brands are filtered and the script can continue.

The code

String url = "https://www.myntra.com/men-tshirts";
driver.get(url);

WebDriverWait wait = new WebDriverWait(driver, 10);
// click Brand search icon
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.filter-search-filterSearchBox"))).click();
// get the Brand search box
WebElement search = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.filter-search-inputBox")));
// search for the first brand
search.sendKeys("HRX by Hrithik Roshan");
// click the brand checkbox
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[text()='HRX by Hrithik Roshan']"))).click();
// wait for the brand to show up as filtered
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[@class='filter-summary-filterList'][contains(.,'HRX by Hrithik Roshan')]")));
// clear the search box
search.clear();
// search for the second brand
search.sendKeys("HERE&NOW");
// click the brand checkbox
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[text()='HERE&NOW']"))).click();
// wait for the brand to show up as filtered
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[@class='filter-summary-filterList'][contains(.,'HERE&NOW')]")));
...
  • Related