无法使用Selenium Webdriver控制幻灯片验证码jquery?

时间:2012-08-15 08:37:43

标签: selenium selenium-webdriver

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

我们从名为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\"]");
    }
}

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

帮我解决这个问题

3 个答案:

答案 0 :(得分:0)

我使用了Actions类的dragAndDropBy方法(java.lang.Object org.openqa.selenium.interactions.Actions)并将滑块水平移动了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();

答案 1 :(得分: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

答案 2 :(得分:0)

如果您的滑块与我的一样

enter image description here

在“滑块轨道”(< 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();
}