无法使用Selenium Webdrive控制滑块验证码jQuery? [英] Unable to control slider captcha jquery using Selenium Webdrive?
问题描述
我想记录我们客户网站上提供的滑块验证码.
I want to record slider captcha given on our client site.
我们已经从名为 http://www.fmylife.com/signup
具有用于注册的滑块验证码
This have slider captcha for registration
我尝试使用Selenium Webdriver Action Builder
I have try to use 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\"]");
}
}
但是我无法使用此代码移动滑块
But I can't move slider using this code
帮我解决这个问题
推荐答案
如果您的滑块像我的滑块
If your slider is like mine
在滑块轨道"(< div>标签为黑色长条)内带有滑块手柄"(值为< a/>标签为框,值为"5ft 5"),然后执行以下操作C#中的代码将可以沿滑块轨道将滑块手柄移动一个百分比.
with a "slider handle" (an <a/> tag as the box with the value "5ft 5") within a "slider track" (a <div> tag as the long black bar) then the following code will in C# will work to move the slider handle a percentage along the slider track.
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();
}
这篇关于无法使用Selenium Webdrive控制滑块验证码jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!