Home > Enterprise >  How to select from a checkbox using selenium java
How to select from a checkbox using selenium java

Time:07-06

I have this website: enter image description here

CodePudding user response:

I can see the element Recently listed is inside the shadow-root so you have use javascript executor here.

Solution

        driver.get("https://magiceden.io/marketplace/primates");

        driver.manage().window().maximize();
        
        Actions action = new Actions(driver);

        action.sendKeys(Keys.PAGE_DOWN).build().perform();

        JavascriptExecutor jse = (JavascriptExecutor) driver;

        WebElement search_box = (WebElement) jse.executeScript("return document.querySelector(\"#content > div.tw-w-full.tw-py-0.sm\\\\:tw-mt-0 > div.tw-flex.tw-relative > div.tw-flex-auto.tw-max-w-full.tw-pt-0 > div.tw-flex.tw-w-full.tw-flex-col.lg\\\\:tw-flex-row.tw-flex-nowrap.lg\\\\:tw-flex-wrap.tw-box-border.tw-min-h-\\\\[52px\\\\].tw-border-0.tw-border-solid.tw-border-gray-300.tw-border-b.tw-px-5.tw-py-0\\\\.5.xl\\\\:tw-sticky.xl\\\\:tw-top-\\\\[79px\\\\].tw-z-10.tw-bg-gray-100 > div.tw-flex.tw-items-center.tw-flex-grow.tw-justify-center.lg\\\\:tw-justify-end > div.me-dropdown-container.lg\\\\:tw-max-w-\\\\[260px\\\\].tw-min-w-\\\\[180px\\\\].tw-flex-grow.tw-text-sm > div.cursor-pointer.position-relative > input\")");

        String js = "arguments[0].click();";

        ((JavascriptExecutor) driver).executeScript(js, search_box);
        
        driver.findElement(By.xpath("//div[normalize-space()='Price: Low to high']")).click();

Import

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

Hope this will solve you issue.

CodePudding user response:

This is quite simple and can be done simply using the native driver properties.

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
            System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")   "\\bin\\geckodriver.exe");
            driver = new FirefoxDriver();
    
            // open url
            driver.manage().window().maximize();
            driver.get("https://magiceden.io/marketplace/primates");
    
            // select checkbox
            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

  • Related