0

enter image description here

I'm trying to use isSelected() method on Amazon website.

I'm trying to tick the check-box of item filter and then read the status of check-box by isSelected() but it always returns false message.

Please help me to overcome this problem.

package seleniumProgram;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.xml.LaunchSuite;

public class Check_isDisplayed_Enabled_Selected extends A_openBrowser {

    public static void main(String[] args) throws InterruptedException {
        openChrome("www.amazon.com");
        WebElement search = driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']"));
        
        System.out.println("Search box is Displayed = "+search.isDisplayed());
        System.out.println("Search box is Enabled = "+search.isEnabled());
        
        driver.navigate().to("https://www.amazon.com/s?k=Nintendo+Switch+Games&i=videogames&bbn=16227133011&rh=n%3A16227133011&dc&ds=v1%3Au77TZDZ%2FgBsOBF9WBVz5WxCcRXqJa6CeSHU8sDYD8Dk&pf_rd_i=23508887011&pf_rd_m=ATVPDKIKX0DER&pf_rd_p=434db2ed-6d53-4c59-b173-e8cd550a2e4f&pf_rd_r=CVZSGC265EZ08YTW0ZDE&pf_rd_s=merchandised-search-5&pf_rd_t=101&qid=1653816583&rnid=2528832011&ref=sr_nr_p_89_1");
        
        WebElement box = driver.findElement(By.xpath("//li[@id='p_89/U&I Entertainment']"));
        System.out.println("U&I Entertainment CheckBox is Selected = "+box.isSelected());
        
        //driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.findElement(By.xpath("//li[@id='p_89/U&I Entertainment']")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        
        WebElement clik = driver.findElement(By.xpath("//body[1]/div[1]/div[2]/div[1]/div[2]/div[1]/div[3]/span[1]/div[1]/div[1]/div[1]/div[3]/ul[1]/li[2]/span[1]/a[1]/div[1]/label[1]/i[1]"));
        System.out.println("U&I Entertainment CheckBox is Selected = "+clik.isSelected());
        driver.close();

1 Answer 1

1

So what you want to do is to check if a check with the label "U&I Entertainment" is selected/checked/clicked.

But, in your code,

WebElement box = driver.findElement(By.xpath("//li[@id='p_89/U&I Entertainment']"));

WebElement clik = driver.findElement(By.xpath("//body[1]/div[1]/div[2]/div[1]/div[2]/div[1]/div[3]/span[1]/div[1]/div[1]/div[1]/div[3]/ul[1]/li[2]/span[1]/a[1]/div[1]/label[1]/i[1]"));

you are actually getting an <li> element and not the actual <input type="checkbox"> within that <li>.

So I changed those 2 lines with,

WebElement box = driver.findElement(By.xpath("//li[@id='p_89/U&I Entertainment']//input[@type='checkbox']"));

WebElement clik = driver.findElement(By.xpath("//li[@id='p_89/U&I Entertainment']//input[@type='checkbox']"));

and I got the desired result.

Here's my code that worked,

try {
        WebDriver driver;
        System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "//geckodriver");
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("https://www.amazon.com");
        driver.manage().window().maximize();
        Thread.sleep(2000);

        WebElement search = driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']"));
            
            System.out.println("Search box is Displayed = "+search.isDisplayed());
            System.out.println("Search box is Enabled = "+search.isEnabled());
            
            driver.navigate().to("https://www.amazon.com/s?k=Nintendo+Switch+Games&i=videogames&bbn=16227133011&rh=n%3A16227133011&dc&ds=v1%3Au77TZDZ%2FgBsOBF9WBVz5WxCcRXqJa6CeSHU8sDYD8Dk&pf_rd_i=23508887011&pf_rd_m=ATVPDKIKX0DER&pf_rd_p=434db2ed-6d53-4c59-b173-e8cd550a2e4f&pf_rd_r=CVZSGC265EZ08YTW0ZDE&pf_rd_s=merchandised-search-5&pf_rd_t=101&qid=1653816583&rnid=2528832011&ref=sr_nr_p_89_1");
            
            WebElement box = driver.findElement(By.xpath("//li[@id='p_89/U&I Entertainment']//input[@type='checkbox']"));
            System.out.println("U&I Entertainment CheckBox is Selected = "+box.isSelected());
            
            driver.findElement(By.xpath("//li[@id='p_89/U&I Entertainment']")).click();
            
            WebElement clik = driver.findElement(By.xpath("//li[@id='p_89/U&I Entertainment']//input[@type='checkbox']"));
            System.out.println("Search box is Displayed = "+clik.isDisplayed());
            System.out.println("U&I Entertainment CheckBox is Selected = "+clik.isSelected());

        Thread.sleep(5000);
        driver.close();
        driver.quit();
    } catch (Exception e) {
        e.printStackTrace();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.