How to Resize an element using Selenium through Java?

问题: I am trying to automate the "realizable concept"(Drag and Drop) in Selenium through the link -http://jqueryui.com/resizable/. I am getting the error: invalid selector: Un...

问题:

I am trying to automate the "realizable concept"(Drag and Drop) in Selenium through the link -http://jqueryui.com/resizable/. I am getting the error:

invalid selector: Unable to locate an element with the xpath expression //div[contains(@class.'demo-frame')].

Could you please let me know if there is any other way of representing the XPATH? Below is my code for the same. Thanks in advance.

public class ResizeExample {
    WebDriver driver;

    @Test
    public void testToResizeElement() {

        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.navigate().to("http://jqueryui.com/resizable/");
        WebDriverWait wait = new WebDriverWait(driver, 5);
        wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame")));
        WebElement resizeableElement = driver.findElement(By.cssSelector(".ui-resizable-handle.ui-resizable-se.ui-icon.ui-icon-gripsmall-diagonal-se"));
        resize(resizeableElement, 50, 50);
    }

    public void resize(WebElement elementToResize, int xOffset, int yOffset) {
        try {
            if (elementToResize.isDisplayed()) {
                Actions action = new Actions(driver);
                action.clickAndHold(elementToResize).moveByOffset(xOffset, yOffset).release().build().perform();
            } else {
                System.out.println("Element was not displayed to drag");
            }
        } catch (StaleElementReferenceException e) {
            System.out.println("Element with " + elementToResize + "is not attached to the page document "  + e.getStackTrace());
        } catch (NoSuchElementException e) {
            System.out.println("Element " + elementToResize + " was not found in DOM " + e.getStackTrace());
        } catch (Exception e) {
            System.out.println("Unable to resize" + elementToResize + " - " + e.getStackTrace());
        }
    }

}

回答1:

To automate the realizable concept(Drag and Drop) with in the webpage http://jqueryui.com/resizable/ through Selenium you can use the following solution:

  • Code Block:

    System.setProperty("webdriver.gecko.driver", "C:\Utility\BrowserDrivers\geckodriver.exe");
    WebDriver driver=new FirefoxDriver();
    driver.get("http://jqueryui.com/resizable/");
    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
    WebElement  target = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se']")));
    //dragAndDropBy(WebElement source, int xOffset, int yOffset) //status: WORKS
    new Actions(driver).dragAndDropBy(target, 50, 50).build().perform();
    System.out.println("Resizing of element Completed");
    
  • Console Output:

    Resizing of element Completed
    
  • Browser Snapshot:

jQuery_resizable

  • 发表于 2019-03-11 02:21
  • 阅读 ( 318 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除