Java selenium webdriver 在左侧移动滑块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28453230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
selenium webdriver move slider on left side
提问by Karim Narsindani
I want to move slider on left side of slider-bar. However, selenium webdriver moves it to right side but it does not move to left side. I want to move slider to 25% of total width of slider-bar. I am using below given code with java 1.8 with selenium 2.44. I have tried all the option using up,down,left,right arrow key but still not able to achieve it.
我想移动滑块左侧的滑块。但是,selenium webdriver 将其移动到右侧,但不会移动到左侧。我想将滑块移动到滑块总宽度的 25%。我在 java 1.8 和 selenium 2.44 中使用下面给出的代码。我已经尝试了使用向上、向下、向左、向右箭头键的所有选项,但仍然无法实现。
I would appreciate your inputs.
我将不胜感激您的投入。
package RandD;
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;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class test{
static WebDriver driver;
public static void main(String[] args)
{
driver = new FirefoxDriver();
driver.get("http://jqueryui.com/slider/");
driver.switchTo().frame(0);
slider();
}
public static void slider(){
WebElement slider = driver.findElement(By.id("slider"));
int width=slider.getSize().getWidth();
Actions move = new Actions(driver);
org.openqa.selenium.interactions.Action action = move.dragAndDropBy(slider, ((width*25)/100), 0).build();
action.perform();
System.out.println("Slider moved");
}
}
采纳答案by Karim Narsindani
Well, I was not able to move the slider using all the possible option using dragAndDropBy and clickAndHold. However, using below snippet I was able to move slider to exact location of slid-bar. I am still wondering what was wrong in above code which does not move the slider to exact location as I was expecting.
好吧,我无法使用所有可能的选项使用 dragAndDropBy 和 clickAndHold 移动滑块。但是,使用下面的代码片段,我能够将滑块移动到滑动条的确切位置。我仍然想知道上面的代码有什么问题,它没有像我期望的那样将滑块移动到确切的位置。
you can set choose value of X is any its depends of width of your slider and if you use for loop to drag pointer on multiple position
您可以设置 X 的选择值取决于滑块的宽度,如果您使用 for 循环在多个位置拖动指针
public static void slider(){
x=10;
WebElement slider = driver.findElement(By.id("slider"));
int width=slider.getSize().getWidth();
Actions move = new Actions(driver);
move.moveToElement(slider, ((width*x)/100), 0).click();
move.build().perform();
System.out.println("Slider moved");
}
回答by Vivek Singh
You need to first switch to the iframe it is contained in i.e.
您需要先切换到它包含在 ie 中的 iframe
<iframe class="demo-frame" src="/resources/demos/slider/default.html">
after that you can perform slide using JavascriptExecutor:
之后,您可以使用 JavascriptExecutor 执行幻灯片:
((JavascriptExecutor) driver).executeScript("document.getElementsByTagName('span')[0].style.left='50%'"); // 50% or whatever you like to provide.
回答by Vivek Singh
I always use this code to move a slide bar:
我总是使用此代码来移动滑动条:
action.click(webElement).build().perform();
Thread.sleep(1000);
for (int i = 0; i < 10; i++) {
action.sendKeys(Keys.ARROW_LEFT).build().perform();
Thread.sleep(200);
}
回答by Josh O'Bryan
I've used this with success.
我已经成功地使用了这个。
var sliderA = driver.FindElementsByCssSelector(".ccwa")[0];
var sliderB = driver.FindElementsByCssSelector(".ccwa")[1];
Actions action = new Actions(driver);
for (int i = 0; i < 5; i++)
{
action.DragAndDropToOffset(sliderA, 50, 0).Build().Perform();
Thread.Sleep(300);
action.DragAndDropToOffset(sliderB, 50, 0).Build().Perform();
Thread.Sleep(300);
}
回答by Raymond Kelly
Simply add a minus to the 25
只需在 25 上加一个减号
org.openqa.selenium.interactions.Action action = move.dragAndDropBy(slider, ((width*-25)/100), 0).build();
- Positive values go to the right
- Negative values go to the left
- 正值向右
- 负值向左移动
The same applys to scrolling pages.
这同样适用于滚动页面。
Also just a note for you. I had some issues with this approach due to the width I wanted to move being a decimal amount. I suggest actually setting the attribute DOM value for the slider instead
也只是给你的笔记。由于我想移动的宽度是小数,我对这种方法有一些问题。我建议实际上为滑块设置属性 DOM 值
回答by Shalini
Below code worked fine for my application:
下面的代码适用于我的应用程序:
WebElement slider = driver.findElement(By.xpath("//input[@id='savingsSlider']"));
for(int i=0;i<=30;i++){
//Slide to RIGHT
slider.sendKeys(Keys.ARROW_RIGHT);
//Slide to LEFT
slider.sendKeys(Keys.ARROW_LEFT);
}
回答by Mladen Radivojevic
The following code works for slide bars with two sliders:
以下代码适用于带有两个滑块的滑动条:
WebElement sliderA = driver.findElement(By.xpath("Left slider xpath"));
Actions move = new Actions(driver);
move.dragAndDropBy(sliderA,10, 0).click();
move.build().perform();
WebElement sliderB = driver.findElement(By.xpath("Right slider xpath"));
move.dragAndDropBy(sliderB, -50, 0).click();
move.build().perform();