0

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

1
  • Please provide html code the checkbox so that we can provide you same. Commented Jul 5, 2022 at 10:26

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Thanks, I'll try that later. Btw what does the line with action.sendkeys() do here?
Please accept the answer if this solve your question, this action.sendKeys(Keys.PAGE_DOWN).build().perform(); will scroll down the page

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.