如何使用Selenium调整Web元素的大小

时间:2013-02-06 09:08:30

标签: selenium selenium-webdriver selenium-rc

我可以使用selenium web驱动程序调整Web元素的大小吗? 如果可以,我如何使用selenium检查操作是否按预期工作。

感谢您的回答。

5 个答案:

答案 0 :(得分:0)

您可以在css中设置高度和宽度,然后使用

getCssValue(java.lang.String propertyName)

有关详细信息,请参阅http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html。我无法找到任何示例代码抱歉!

拉​​结

答案 1 :(得分:0)

我发现了以下内容:
- 拖拽在Selenium中正常工作 - 硒中的元素没有调整大小的方法
- 如果一个元素是可调整大小的,在我的情况下是一个框架,那么它是HTML网站中用于调整大小的元素(该元素可能是不可见的,但是如果你对它进行mofe鼠标光标会改变)
- 您只需识别此元素并对其执行拖放操作

答案 2 :(得分:0)

我还必须使用selenium调整\ dragAndDrop的大小..所以在经过一些研究后我找到了this site,它告诉你如何在没有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();

我尝试过它并且有效。至于拖动你可以使用:

Actions builder = new Actions(driver);

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

dragAndDrop.perform();

Avinoam

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

}