简体   繁体   English

在Selenium WebDriver 3.6中无法拖放

[英]Unable to Drag & Drop in Selenium WebDriver 3.6

I am trying to drag & drop but its not working. 我正在尝试拖放,但无法正常工作。

Here is my code. 这是我的代码。

Please help, I have applied so much time on this, but it is still not working. 请帮助,我已经花了很多时间在此上,但是仍然无法正常工作。

Chrome Version: 62.0.3202.75 Chrome版本:62.0.3202.75
ChromeDriver : 2.33 ChromeDriver:2.33
Selenium : 3.6 硒3.6

public class Drag_And_Drop {
static String baseURl="https://www.google.com";
static WebDriver driver;

@BeforeMethod
public void openBrowser() {     
    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/drivers/chromedriver.exe");
    driver=new ChromeDriver();
    driver.get(baseURl);
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
}

@Test
public void verifyCount() {

    WebElement searchBox = driver.findElement(By.xpath(".//*[@id='lst-ib']"));
    searchBox.sendKeys("jqwidget drag and drop");
    searchBox.sendKeys(Keys.ENTER);     

    WebElement link = driver.findElement(By.linkText("jQuery DragDrop, DragDrop plug-in, Drag and Drop ... - jQWidgets"));
    link.click();       

    driver.switchTo().frame(0);

    WebElement source = driver.findElement(By.xpath(".//*[@id='jqxWidgete3128591f541']"));
    source.click();

    WebElement target = driver.findElement(By.xpath(".//*[@id='cart']"));       

    Actions actions = new Actions(driver);
    actions.dragAndDrop(source, target).build().perform();
}   

@AfterMethod
public void closeBrowser() {
        driver.quit();
}
}

The problem in your code is here: 您的代码中的问题在这里:

WebElement source = driver.findElement(By.xpath(".//*[@id='jqxWidgete3128591f541']"));

You are trying to find the element with the id but, actually, this id is always different. 您正在尝试查找具有ID的元素,但实际上,此ID总是不同的。 If you open 2 different browser and try to inspect the same element, you will notice this. 如果您打开2个不同的浏览器并尝试检查相同的元素,则会注意到这一点。

You could try to locate all the elements with classname "draggable-demo-product jqx-rc-all jqx-draggable" , and after add the elements that you want. 您可以尝试找到所有类名称为“ draggable-demo-product jqx-rc-all jqx-draggable”的元素,然后添加所需的元素。

In the following example, all the elements are added: 在以下示例中,添加了所有元素:

    //WebElement source = driver.findElement(By.xpath(".//*[@id='jqxWidgete3128591f541']"));
    WebElement source = driver.findElement(By.xpath("//div[@id='shop']"));
    List<WebElement> listDraggableElement=source.findElements(By.xpath("//div[@class='draggable-demo-product jqx-rc-all jqx-draggable']"));

    WebElement target = driver.findElement(By.xpath(".//*[@id='cart']"));
    Actions actions = new Actions(driver);
    for(WebElement el: listDraggableElement)
    {
        System.out.println(el.getText());
        actions.dragAndDrop(el, target).build().perform();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM