为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过 Java 使用 Selenium 调整元素的大小?

如何通过 Java 使用 Selenium 调整元素的大小?

慕后森 2022-07-27 16:45:02
我正在尝试通过链接 - http://jqueryui.com/resizable/自动化 Selenium 中的“可实现概念”(拖放)。我收到错误消息:invalid selector: Unable to locate an element with the xpath expression //div[contains(@class.'demo-frame')].您能否让我知道是否有任何其他方式来表示 XPATH?下面是我的代码。提前致谢。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 回答

?
慕码人8056858

TA贡献1803条经验 获得超6个赞

要通过Selenium在网页http://jqueryui.com/resizable/中自动实现可实现的概念(拖放) ,您可以使用以下解决方案:


代码块:


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");

控制台输出:


Resizing of element Completed

浏览器快照:

//img1.sycdn.imooc.com//62e0fb2f000186f513630765.jpg

查看完整回答
反对 回复 2022-07-27
  • 1 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信