nirvana
nirvana

Reputation: 195

Selenium WebDriver resize textarea

Using Selenium webdriver with Java how can I resize a textarea/ I want to expand the textarea by dragging the right-bottom corner of the element.

I have tried something like this, but it doesnt change the element at all

new Actions(webdriver).dragAndDropBy(element, height, width).perform()

Upvotes: 0

Views: 3634

Answers (3)

Aventador
Aventador

Reputation: 52

This can be done with JavaScriptExecutor interface. JavaScriptExecutor is an interface which provides mechanism to execute Javascript through selenium webdriver with the help of this you can change rows and cols attributes of textarea hence textarea can be resize with it. Hope this will help you out -

public static void main(String[] args) {

    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile a = profile.getProfile("default");
    WebDriver driver = new FirefoxDriver(a);
    driver.get("http://localhost/testfolder/textarea.html");

    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("document.getElementById('textarea').setAttribute('rows', '30')");
    js.executeScript("document.getElementById('textarea').setAttribute('cols', '50')");



}

just locate textarea element with an element locator then set 'rows' and 'cols' attribute values.

I hope this helps you.

Upvotes: 0

Ajay
Ajay

Reputation: 187

I think the reason your code was not working because build() method was not included can you try this and tell if its now working-

new Actions(webdriver).dragAndDropBy(element, height, width).build().perform();

Upvotes: 0

Cad
Cad

Reputation: 374

I do not have anything to test this issue on, but i'm guessing the reason the dragAndDropBy method you used didn't work because it would not be clicking the bottom-right corner of the element. I believe you would need something like:

Actions action = new Actions(driver);
action.moveToElement(toElement, xOffset, yOffset); //moves to bottom right corner
action.clickAndHold();
action.moveByOffset(xOffset, yOffset); 
action.release();
action.perform();

The offsets depend on the size of the text area you mentioned. You can look into more on the Action class at: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html. Hopefully this helps.

Upvotes: 4

Related Questions