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

vkc1a9a2  于 2022-11-10  发布在  其他
关注(0)|答案(6)|浏览(165)

我可以使用Selify Web驱动程序调整Web元素的大小吗?如果这是可能的,我如何使用Selify检查操作是否如预期的那样工作。
谢谢你的回答。

fnatzsnv

fnatzsnv1#

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

ql3eal8s2#

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

getCssValue(java.lang.String propertyName)

有关更多详细信息,请参见http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html。我找不到任何示例代码,对不起!
瑞秋

unguejic

unguejic3#

我还必须使用Selify来调整\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();

阿维努阿姆

zaq34kh6

zaq34kh64#

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

}
wbrvyc0a

wbrvyc0a5#

我已经为其中一个网站做了调整代码。

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)", "");
    }
}
6l7fqoea

6l7fqoea6#

我发现了以下几点:

  • 在Selify中拖放工作正常
  • selenium 中元素没有调整大小的方法
  • 如果一个元素是可调整大小的,在我的例子中是一个框架,那么在HTML站点中有一个元素可以调整大小(该元素可能是不可见的,但如果你移动到它上面,鼠标光标就会改变)
  • 您只需识别此元素并在其上执行拖放操作

相关问题