如何使用SeleniumWebdriver在滑块中滑动到特定点



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

我的问题是:

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

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

我正在使用JAVA和Web驱动程序,而且我是一个初学者,所以如果下面的代码有任何错误,请耐心等待:)

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

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

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

我只在Firefox上测试过它。它应该在铬以及中工作良好

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

相关内容

  • 没有找到相关文章

最新更新