2

我想记录我们客户网站上提供的滑块验证码。

我们从名为http://www.fmylife.com/signup的其他网站获得了这个概念

这有用于注册的滑块验证码

我尝试使用 selenium webdriver action builder

public class TestFmylife {
    WebDriver driver;
    Selenium selenium;

    @BeforeMethod
    public void startSelenium() {
        driver = new FirefoxDriver();
        selenium = new WebDriverBackedSelenium(driver, "http://www.fmylife.com/");
        driver.manage().window().maximize();
    }

    @AfterMethod
    public void stopSelenium() {
        driver.close();
    }

    @Test
    public void testFmylife() {
        selenium.open("/");
        selenium.click("link=Sign up");
        selenium.waitForPageToLoad("30000");
        selenium.type("name=login", "testfmylife");
        selenium.type("name=pass", "123@fmylife");
        selenium.type("name=passc", "123@fmylife");
        selenium.type("name=mail", "testfmylife@gmail.com");

        Point MyPoint= driver.findElement(By.xpath("//*[@id='bgSlider']")).getLocation();

        WebElement someElement = driver.findElement(By.xpath("//*[@id='bgSlider']"));

        System.out.println(MyPoint.x+"--------"+MyPoint.y);


        Actions builder = new Actions(driver);

        Action dragAndDrop =  builder.clickAndHold(someElement).moveByOffset(MyPoint.x,(MyPoint.y + 100)).release().build();

        dragAndDrop.perform();

        selenium.click("css=div.form > div.ok > input[type=\"submit\"]");
    }
}

但我无法使用此代码移动滑块

帮我解决这个问题

4

3 回答 3

0
Actions builder = new Actions(driver);

Action dragAndDrop = builder.clickAndHold(someElement)
   .moveToElement(otherElement)
   .release(otherElement)
   .build();

dragAndDrop.perform();

更多信息可以在 - http://code.google.com/p/selenium/wiki/AdvancedUserInteractions找到

您可以按如下方式使用定位器 -

String xto=Integer.toString(LocatorTo.getLocation().x);
String yto=Integer.toString(LocatorTo.getLocation().y);

工作代码-

WebDriver driver = new InternetExplorerDriver();
driver.get("http://jqueryui.com/demos/slider/");
//Identify WebElement
WebElement slider = driver.findElement(By.xpath("//div[@id='slider']/a"));

//Using Action Class
Actions move = new Actions(driver);
Action action = move.dragAndDropBy(slider, 30, 0).build();
action.perform();

driver.quit();

来源 - https://gist.github.com/2497551

于 2012-08-21T07:30:27.463 回答
0

如果你的滑块和我的一样

在此处输入图像描述

在“滑块轨道”(一个 <div> 标记作为长黑条)内有一个“滑块手柄”(一个 <a/> 标签作为值为“5ft 5”的框),那么以下代码将在 C# 中努力将滑块手柄沿滑块轨道移动一个百分比。

public void SetSliderPercentage(string sliderHandleXpath, string sliderTrackXpath, int percentage)
{
    var sliderHandle = driver.FindElement(By.XPath(sliderHandleXpath));
    var sliderTrack = driver.FindElement(By.XPath(sliderTrackXpath));
    var width = int.Parse(sliderTrack.GetCssValue("width").Replace("px", ""));
    var dx = (int)(percentage / 100.0 * width);
    new Actions(driver)
                .DragAndDropToOffset(sliderHandle, dx, 0)
                .Build()
                .Perform();
}
于 2013-05-14T08:00:02.973 回答
0

我使用了 Actions 类 (java.lang.Object org.openqa.selenium.interactions.Actions) 的 dragAndDropBy 方法,并将滑块水平移动了 200 个点。请尝试以下代码:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.fmylife.com/signup");
WebElement slider = driver.findElement(By.xpath(".//*[@id='Slider']"));
Actions builder = new Actions (driver);
builder.dragAndDropBy(slider, 200, 0).build().perform();
于 2012-08-16T04:09:33.563 回答