如何将滑块拖动到某个字符串

时间:2018-05-21 19:55:49

标签: java selenium xpath slider

在此页面上:https://www.wedoqa.com/ 我必须将滑块滑动到certen引号。 (Eversave,Simplymap和模式发布)

现在我的代码看起来像这样:

package test;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test1 {

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.gecko.driver", "C:/Users/goran/Desktop/Alas/geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");

        WebElement search = driver.findElement(By.name("q"));
        search.sendKeys("wedoqa.com\n");
        search.submit();

        Thread.sleep(5000);

        WebElement firstResult = driver.findElement(By.xpath("//h3[@class='r']/a"));
        firstResult.click();

        Thread.sleep(1000);

        WebElement testimonialsReferences = driver.findElement(By.id("testimonials"));
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", testimonialsReferences);

        Thread.sleep(2000);     

        //HERE I NEED TO FIND ELEMENT AND SWIPE TO CERTAIN QUOTE
    }
}

所以,那些是: enter image description here

enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试使用拖放功能 见下面的方法

/**
 * @author mbn
 * @Date 05/01/2018
 * @Purpose This method will perform a drag and drop
 * @param fromWebElement --> element to drag from
 * @param toWebElement --> element to release to
 * @return N/A
 */
public static void dragAndDrop_Method2(WebElement fromWebElement, WebElement toWebElement) {

    Actions builder = new Actions(driver);
    Action dragAndDrop = builder.clickAndHold(fromWebElement).moveToElement(toWebElement).release(toWebElement)
            .build();
    dragAndDrop.perform();
}

或此方法

/**
 * @author mbn
 * @Date 05/01/2018
 * @Purpose This method will perform a drag and drop
 * @param fromWebElement --> element to drag from
 * @param toWebElement --> element to release to
 * @return N/A
 */
public static void dragAndDrop_Method3(WebElement fromWebElement, WebElement toWebElement)
        throws InterruptedException {

    Actions builder = new Actions(driver);
    builder.clickAndHold(fromWebElement).moveToElement(toWebElement).perform();
    Thread.sleep(2000);
    builder.release(toWebElement).build().perform();
}

答案 1 :(得分:0)

使用滚动来实现此目的。请尝试以下代码。

WebElement element = driver.findElement(By.id("testimonials"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();

希望这有帮助!