如何使用Selenium Webdriver滑动到滑块中的特定点

时间:2015-06-21 19:21:22

标签: java selenium slider

我面临的问题特定于网站(https://www.eliterewards.com/ip-er/app/Welcome),该网站使用滑块并位于滑块的不同位置;我们有弹出窗口。

我的问题是:

  1. 如何识别我们弹出的所有位置(像素)?手动我们可以看到,但我对代码感兴趣,它告诉我滑块中的确切像素位置。

  2. 如何使用Selenium Webdriver将控件移动到特定点以便弹出窗口?

  3. 我正在使用JAVA和Webdriver,我是初学者,所以如果下面的代码中有任何错误,请耐心等待:)

    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();
                }
            }
        }
    }
    

    当我执行上面的代码时,指针几乎从滑块的中间开始(我认为它应该从滑块的开头开始)并到达滑块的末尾。有人有什么建议吗?

2 个答案:

答案 0 :(得分:0)

我注意到您可以通过向滑块指针发送左右箭头键来移动滑块 因此,当指针每个发送键移动1次时,我发送箭头键一百次。 然后检查弹出是否存在我检查了类drag_popupcontainer是否显示

我只用Firefox测试过它。它也应该在chrome中正常工作

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();
    }
}

答案 1 :(得分:0)

@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);
            }
        }
    }