How to select from a checkbox using selenium java

Issue

I have this website: https://magiceden.io/marketplace/primates. On the website there’s a default option of "Recently listed". How can I use java selenium to change that from the checklist to "price: low to high"? enter image description here

Solution

This is quite simple and can be done simply using the native driver properties without over complicating it with using Javascript, Actions or Scroll.

You are facing a problem because the element you are trying to locate is called an "svg element" so it will not support standard xpath format. So rather than focusing on the checkbox, pin-point the non svg element instead (the div class the element is in).

The code will then look like this:

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

    class App {
        public static WebDriver driver;
    
        public static void main(String[] args) {
            // set your driver here
            ...

            // select checkbox
            driver.get("https://magiceden.io/marketplace/primates");
            driver.findElement(By.xpath("//input[@value='Recently Listed']")).click();
            driver.findElement(By.xpath("//div[normalize-space()='Price: Low to high']")).click();
        }
    }

The final result will look like this:

[output

Answered By – Gautam Nag

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published