简体   繁体   中英

How to slide to a specific point in a slider using Selenium Webdriver

The problem I am facing is specific to a website ( https://www.eliterewards.com/ip-er/app/Welcome ) which uses a slider and in different points of the slider; we have pop ups.

My questions are:

  1. How can I identify in which all positions (pixels rather) we have pop ups? Manually we can see that, but I am interested in code which tells me about the exact pixel positions in the slider.

  2. How can I move control to the specific point using Selenium Webdriver so that pop up appears?

I am using JAVA and Webdriver, and I am a beginner, so if there is any mistake in the code below, please bear with me :)

package cmp;

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

public class DrangAndDrop {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver",
                "D://Selenium//chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        driver.get("https://www.eliterewards.com/ip-er/app/Welcome");

        WebElement slider = driver.findElement(By.id("drag_slider"));

        WebElement first_element = driver.findElement(By.id("reward_12000")); // The
        // "View Details"
        // button
        // on
        // Reward
        // Point
        // 12000.

        System.out.println(slider.getSize());

        Actions action = new Actions(driver);

        for (int i = 0; i <= 890; i++) {
            action.dragAndDropBy(slider, i, 0).perform();

            if (first_element.isDisplayed()) {
                first_element.click();
            }
        }
    }
}

When I execute the above code, the pointer starts almost from the middle of the slider (I thought it should start from the beginning of the slider though) and reaches the end of the slider. Anyone has any suggestions please?

I noticed that you can move the slider by sending the right and left arrow key to the slider pointer So I send the arrow key for hundred times as the pointer moves by 1 per sendkey. Then to check if popup is present I checked the class drag_popupcontainer is displayed or not

I have tested it with Firefox only.It should work fine in chrome as well

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DrangAndDrop {

    public static void main(String[] args) {

//        System.setProperty("webdriver.chrome.driver",
//                "D://Selenium//chromedriver.exe");
        WebDriver driver = new FirefoxDriver();

        driver.get("https://www.eliterewards.com/ip-er/app/Welcome");

        WebElement slider = driver.findElement(By.cssSelector("#drag_slider>a"));
        slider.click();

        for (int i = 0; i < 100; i++) {
            if (isPopupPresent(driver)) {
                //do your thing
                System.out.println("Popup present");
            }
            slider.sendKeys(Keys.ARROW_RIGHT);
        }
        driver.quit();
    }

    public static Boolean isPopupPresent(WebDriver driver) {
        WebElement popup = driver.findElement(By.className("drag_popupcontainer"));
        return popup.isDisplayed();
    }
}
@Test
    public void Slider_Test() throws InterruptedException {
        int fullSlider = 35000, startSlider = 5000, adjustmentSlider = 2, curentPercent = 0;

        driver.get("https://www.eliterewards.com/ip-er/app/Welcome");

        WebElement slider = driver
                .findElement(By.cssSelector("#drag_slider>a"));
        slider.click();

        List<WebElement> rewards = driver.findElements(By
                .cssSelector("div[id*=reward]"));

        // 1 - Move the slider back to 0% - 5,000
        for (int i = 0; i < 100; i++) {
            slider.sendKeys(Keys.ARROW_LEFT);
        }

        for (WebElement reward : rewards) {
            driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
            List<WebElement> hasReward = reward.findElements(By.cssSelector("div"));
            if (hasReward.size() > 0) {

                // 2 - Calculate Slider Percentage for the reward
                int rewardPrice = Integer.parseInt(reward.getAttribute("id")
                        .replace("reward_", "")), rewardSliderPercent = Math
                        .round((rewardPrice - startSlider) * 100 / fullSlider
                                - adjustmentSlider), steps = rewardSliderPercent
                        - curentPercent;

                // 3 - Move to the reward
                for (int i = 0; i < steps; i++) {
                    slider.sendKeys(Keys.ARROW_RIGHT);
                }

                curentPercent = rewardSliderPercent;
                Thread.sleep(2000);
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM