user1882216
user1882216

Reputation: 147

How to resize a web element using Selenium

can I resize a web element using selenium web driver? If this is possible, how do I check whether the action did work as expected using selenium.

Thanks for your answers.

Upvotes: 2

Views: 14666

Answers (6)

user1882216
user1882216

Reputation: 147

I've found out the following:

  • Drag and Drop works ok in Selenium
  • There is no resize method on elements in selenium
  • If an element is resizable, a frame in my case, there is an element in the HTML site for resizing (the element might be invisible but the mouse cursor changes if you move over it)
  • You just have to identify this element and perform a Drag and Drop action on it

Upvotes: 0

Aditya Nadkarni
Aditya Nadkarni

Reputation: 1

I have done the resize code for one of the Websites.

package com.demoqa.interactions;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;


public class TC025_Interactions_Resizable 
{
    public static WebDriver driver;
    public static void main(String[] args) throws InterruptedException 
    {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\aadityn\\Downloads\\chromedriver_win32\\chromedriver.exe");
        driver= new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://demoqa.com/resizable");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        TC025_Interactions_Resizable tt=new TC025_Interactions_Resizable();
                
        //scroll down to the desired element
        tt.Scroll();
        
        //clicking on tabs 
        WebElement resizeableElement = driver.findElement(By.xpath("//div[@id='resizableBoxWithRestriction']/span"));
        resizable(resizeableElement, 200, 100);
        
        Thread.sleep(2000L);
        
        //closing the session
    //  driver.quit();
        
    }
    
    
    public static void resizable(WebElement elementToResize, int xOffset, int yOffset) 
    {
                try
                {
                    Actions act= new Actions(driver);
                    act.clickAndHold(elementToResize).moveByOffset(xOffset, yOffset).release().build().perform();
                }
                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());
                }
                
    }
    
    public void Scroll()
    {
        JavascriptExecutor jse= (JavascriptExecutor)driver;
        jse.executeScript("window.scrollBy(0,300)", "");
    }
}

Upvotes: 0

Imrankhan
Imrankhan

Reputation: 1

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

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());
        }
    }

}

Upvotes: 0

ramadan.jr
ramadan.jr

Reputation: 21

public void Resize(IWebElement element, int offsetX, int offsetY = 0)
{   
    int width = element.Size.Width;            
    Actions action = new Actions(driver);
    action.MoveToElement(element, width, 1);
    action.ClickAndHold().MoveByOffset(offsetX, offsetY).Release();
    action.Build().Perform();            
}

Upvotes: 2

Avinoam Ashkenazi
Avinoam Ashkenazi

Reputation: 1

I also had to resize\dragAndDrop using selenium.. so after some research i found this site , which tells you how to do resize without Js:

WebElement resizeable = this.getDriver().findElement(By.cssSelector(" enter your selector, mine was div.resize"));

Action resize = action.clickAndHold(resizeable).moveByOffset(X offset, Y offset).release().build();

resize.perform();

I tried it and it worked. as for drag you can use:

Actions builder = new Actions(driver);

Action dragAndDrop = builder.clickAndHold(someElement)
   .moveToElement(otherElement)
   .release(otherElement)
   .build();

dragAndDrop.perform();

Avinoam

Upvotes: 0

Rachel Gallen
Rachel Gallen

Reputation: 28553

you can set the height and width in css and then use

getCssValue(java.lang.String propertyName)

see http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html for more details. I wasn't able to find any sample code sorry!

Rachel

Upvotes: 0

Related Questions