硒webdriver在左侧移动滑块 [英] selenium webdriver move slider on left side
问题描述
我想移动滑块左侧的滑块。但是,硒webdriver将它移动到右侧,但它不会移动到左侧。我想将滑块移动到滑块总宽度的25%。我使用下面给出的代码与java 1.8与selenium 2.44。我已尝试使用向上,向下,向左,向右箭头键的所有选项,但仍然无法实现。
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.
我将非常感谢您的投入。
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");
}
}
推荐答案
好,我无法使用dragAndDropBy和clickAndHold使用所有可能的选项移动滑块。但是,使用下面的代码片段,我可以将滑块移动到滑动条的确切位置。我仍然想知道上面的代码有什么问题,它没有像我期望的那样将滑块移动到精确的位置。
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.
你可以设置选择X的值是否取决于它滑块的宽度,如果你使用for循环将指针拖到多个位置
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
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");
}
这篇关于硒webdriver在左侧移动滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!