如何在水平进度条上滑动滑块 网址:http://jqueryui.com/slider/ 我尝试滚动滑块,但它对我不起作用。我使用Java的代码如下:
public class TestClass
{
public static void main(String[] args) {
File file = new File("D:\\Driver\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.get("http://jqueryui.com/slider/");
WebElement slider = driver.findElement(By.xpath("//div[@id='slider']/a"));
Actions move = new Actions(driver);
Action action = (Action) move.dragAndDropBy(slider, 30, 0).build();
((Actions) action).perform();
}
}
控制台
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == html body div#slider.ui-slider a.ui-slider-handle (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 286 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:09:54'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_13'
Driver info: driver.version: RemoteWebDriver
Session ID: 79edc751-a9a3-4ecb-a171-d2b43c088a66
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:498)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:268)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:345)
at org.openqa.selenium.By$ByCssSelector.findElement(By.java:406)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:260)
at testpakage.testclass.main(testclass.java:30)
尝试发送密钥!
public class TestClass
{
public static void main(String[] args) {
File file = new File("D:\\Driver\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.get("http://jqueryui.com/slider/");
WebElement slider = driver.findElement(By.xpath("//div[@id='slider']/a"));
for (int i = 1; i <= "how many times you want" ; i++) {
slider.sendKeys(Keys.ARROW_RIGHT);
}
}
}
尝试下面的代码。
public class TestClass
{
public static void main(String[] args) {
File file = new File("D:\\Driver\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.get("http://jqueryui.com/slider/");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://jqueryui.com/slider/");
driver.switchTo().frame(0); //need to switch to this frame before clicking the slider
WebElement slider = driver.findElement(By.xpath("//div[@id='slider']/span"));
Actions move = new Actions(driver);
Action action = (Action) move.dragAndDropBy(slider, 30, 0).build();
action.perform();
}
//Try this code
WebElement e = driver.findElement(By.className("Class name of Dragger"));
Actions move = new Actions(driver);
move.moveToElement(e).clickAndHold().moveByOffset(0,250).release().perform();
Thread.sleep(3500);
我刚刚尝试了另一种方法来处理滑块。
我的代码:
public void sliderCode(int iSliderAdjustValue){
File file = new File("D:\\Driver\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.get("http://jqueryui.com/slider/");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://jqueryui.com/slider/");
driver.switchTo().frame(0); //need to switch to this frame before clicking the slider
WebElement slider = driver.findElement(By.xpath("//div[@id='slider']/a"));
Actions builder= new Actions(driver);
int iCount = 0;
if (iRange > 0) {
for(iCount = 0; iCount < iRange; iCount++) {
builder.moveToElement(slider).click(slider).sendKeys(Keys.ARROW_UP).perform();
}
} else {
for (iCount = 0; iCount > iRange; iCount--) {
builder.click(slider).sendKeys(Keys.ARROW_DOWN) .perform();
}
}
}
我认为幻灯片是使用css样式格式来滚动的 所以我们改变元素的样式
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement a = driver.findElement(By.xpath("//div[@id='slider-1']/a"));
js.executeScript("arguments[0].setAttribute('style', 'left: 30%;')",a);
Use the below method to forwarding and Reverse seek on player
public void dragByPixelElement(WebElement element, int x, int y) {
WebElement element = driver.findElements(By.xpath("sliderPointXpath"));
Actions act = new Actions(driver);
act.dragAndDropBy(element, 200, 500).perform();
}
For forward seek use positive x value and for reverse seek use negative value as I used in this code :)
使用此代码
Actions dragger = new Actions(driver);
WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("**xpath of slider**"));
int numberOfPixelsToDragTheScrollbarDown = 5000;
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
这段代码对我来说绝对没问题 处理网站的滑块:homeshop18.com 试试这个代码:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.homeshop18.com/fashion-jewellery/category:15143/filter_Theme:%28%22Traditional+Wear%22+%22Cuff+%26+Kada%22+%22Daily+Wear%22+%22Maang+Tikka%22+%22Openable+Round%22+%22Round%22+%22Openable+Oval%22%29/sort:Popularity/inStock:true/?it_category=HP&it_action=JW-HPSP01&it_label=HP-HPSP01-131021235900-PD-JW-ZC-VK-SC_DiwaliFestWeddingJewellery&it_value=0");
WebElement Slider = driver.findElement(By.xpath("//*[@id='slider-range']/a[1]"));
Thread.sleep(3000);
Actions moveSlider = new Actions(driver);
Action action = moveSlider.dragAndDropBy(Slider, 30, 0).build();
action.perform();
在这里使用 CSS 会很好
public class testclass
{
public static void main(String[] args)
{
File file = new File("D:\\Driver\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.get("http://jqueryui.com/slider/");
WebElement slider = driver.findElement(By.cssSelector("css=div#slider > a"));
Actions move = new Actions(driver);
Action action = (Action) move.dragAndDropBy(slider, 30, 0).build();
((Actions) action).perform();
}
}
package UKRTEST;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Ex_DragndDrop {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "E:\\Chrome Driver\\chromedriver.exe");
driver = new ChromeDriver();
String URL = "http://www.seleniumeasy.com/test/drag-drop-range-sliders-demo.html";
driver.get(URL);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
Thread.sleep(3000);
Actions builder = new Actions(driver);
WebElement draggablePartOfScrollbar = driver
.findElement(By.xpath("/html/body/div[2]/div/div[2]/section/div[1]/div[1]/div/input"));
// To get pixel count of Slider X axis and Y axis(267,25)
Dimension sliderWidth = draggablePartOfScrollbar.getSize();
System.out.println(sliderWidth);
// We need to give below value in +00 to move slider to 50 and +130 to move slider to 100,-100 to move slider to 12
int numberOfRangeToDragTheSlider = -100;
builder.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(numberOfRangeToDragTheSlider, 0)
.release().perform();
}
}
尝试使用这个(最新)
public class TestSliders {
public static WebDriver driver;
public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.get("https://jqueryui.com/slider/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.switchTo().frame(0);
WebElement slider = driver.findElement(By.xpath("//*[@id=\"slider\"]/span"));
new Actions(driver).dragAndDropBy(slider, 40, 0).perform();
}
}
以下代码对我有用。请确保您转到滑块的位置。并添加perform()。它 100% 对我有用。
Actions act=new Actions(driver);
WebElement slideElement=driver.findElement(
By.xpath("//*[@id=\"slider\"]/span"));
act.dragAndDropBy(slideElement, 100, 0).build().perform();