如何使用selenium java向左和向右移动滑块

时间:2017-04-28 08:56:24

标签: java selenium selenium-webdriver selenium-rc

我想使用selenium java从一个步骤拖到另一个步骤。我在stackoverflow上引用了很多帖子但是没有一个与我的情况相关我认为是这样。以下是我的HTML代码:

 <div style="width: 100%; height: auto; font-weight: normal; font-family: Tahoma; font-size: 8pt; font-style: normal; text-decoration: none;" class="QvSliderObject">
    <div style="width: 711px; height: 61px;">
    <canvas height="61" width="711" style="width: 711px; height: 61px;">
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 21px; top: 11px; width: 8px; height: 14px;">1</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 58px; top: 11px; width: 8px; height: 14px;">2</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 95px; top: 11px; width: 8px; height: 14px;">3</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 132px; top: 11px; width: 8px; height: 14px;">4</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 168px; top: 11px; width: 8px; height: 14px;">5</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 205px; top: 11px; width: 8px; height: 14px;">6</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 242px; top: 11px; width: 8px; height: 14px;">7</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 279px; top: 11px; width: 8px; height: 14px;">8</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 316px; top: 11px; width: 8px; height: 14px;">9</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 350px; top: 11px; width: 14px; height: 14px;">10</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 386px; top: 11px; width: 14px; height: 14px;">11</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 423px; top: 11px; width: 14px; height: 14px;">12</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 460px; top: 11px; width: 14px; height: 14px;">13</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 497px; top: 11px; width: 14px; height: 14px;">14</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 534px; top: 11px; width: 14px; height: 14px;">15</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 571px; top: 11px; width: 14px; height: 14px;">16</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 607px; top: 11px; width: 14px; height: 14px;">17</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 644px; top: 11px; width: 14px; height: 14px;">18</div>
    <div style="position: absolute; font-size: 8pt; overflow: visible; color: rgb(54, 54, 54); font-family: Tahoma; font-weight: normal; text-align: center; left: 681px; top: 11px; width: 14px; height: 14px;">19</div></canvas> 
<canvas style="width: 8px; height: 12px; position: absolute; left: 646px; top: 40px; cursor: inherit;" width="8" height="12"></canvas>
    <canvas style="width: 10px; height: 12px; position: absolute; left: 696px; top: 40px;" width="10" height="12"></canvas>
    <canvas style="width: 10px; height: 12px; position: absolute; left: 5px; top: 40px;" width="10" height="12"></canvas></div></div> 

以下是我的java代码:

WebElement sliderLocation = driver.findElement(By.xpath("(//*[@id='64']//following::canvas)[2]"));
        // Used points class to get x and y coordinates of element.
        Point classname = sliderLocation.getLocation();
        int xcordi = classname.getX();
        int ycordi = classname.getY();
        int width = sliderLocation.getSize().getWidth();
        System.out.println(
                "Element's Position from left side of window " + xcordi + "and from right side of Window " + ycordi);
        try {
            Thread.sleep(3000);
            Actions move = new Actions(driver);
            Action action1 = (Action) move.dragAndDropBy(sliderLocation, 60, 0).build();
            action1.perform();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  

并尝试了在thisthis链接上建议的所有可能的解决方案,但仍无用。我应该在坐标部分设置什么。默认情况下,它位于18,窗口左侧的坐标位置为1319,窗口右侧的坐标位置为252 我附上了该滚动条的图像。假设我想从18拖到17,那么我该怎么做呢?请帮我 。我被困住了。
enter image description here

1 个答案:

答案 0 :(得分:1)

以下代码会将滑块移动到“17”比例:

WebElement sliderLocation = driver.findElement(By.xpath("(//*[@id='64']//following::canvas)[2]"));
  WebElement elem17 = driver.findElement(By.xpath("//*[@id='64']/div[2]/div[1]/div/div[17]"));

Actions action = new Actions(driver);
action.clickAndHold(sliderLocation).moveToElement(elem17).release(sliderLocation).build().perform();

我假设sliderLocation引用了这个元素enter image description here