我正在通过Selenium自动化一些网站测试,一步需要调整表格的列宽(减小列宽),(表格看起来像excel表格一样)。
我怎样才能实现自动化?
从stackoverflow,对于类似的问题,有人建议使用Action,例如
action.clickAndHold(elementToResize).moveByOffset(xOffset, yOffset).release()
但是,如果我在UI中使用鼠标,如何通过css定位要调整大小的元素(要调整大小的元素是列的边缘)?
或者我应该使用executeScript?如果是这样,有人可以给我一个例子吗?
提前致谢
答案 0 :(得分:1)
以下是在selenium 3.0中使用Actions调整大小的简单示例。
package testActionsJquery;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class testResizable {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver","C:\\Users\\Chetan\\Desktop\\Chetan\\eclipse-jee-oxygen-1a-win32-x86_64\\geckodriver-v0.19.1-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://jqueryui.com/resizable/");
driver.switchTo().frame(0);
//Get xpath using firebug.
WebElement resize = driver.findElement(By.xpath(".//*[@id='resizable']/div[3]"));
Actions action = new Actions(driver);
Thread.sleep(3000L);
//Using appropriate x and y axis coordinates
action.dragAndDropBy(resize, 400, 200).perform();
}
}
答案 1 :(得分:0)
在Selenium Webdriver中使用Action类。
例如
WebElement.getSize().getWidth()
&获取Element的宽度和高度.getHeight()
。Actions.moveToElement(toElement, full_width, half_height)
将鼠标移动到元素的右中间边缘。.clickAndHold()
& .moveByOffset(offset_to_move, 0)
& .release()
要调整大小。使用表头th
元素替换以下示例中的resizeable元素以调整表的大小。
public static void main(String[] args) {
//To open chrome browser.
System.setProperty("webdriver.chrome.driver","C:\\Windows\\System32\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
//To wait until the element get visible or invisible.
WebDriverWait wait = new WebDriverWait(driver, 25);
driver.get("https://jqueryui.com/resources/demos/resizable/default.html");
//For wait until the element get visible.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("resizable")));
WebElement resize_div = driver.findElement(By.id("resizable"));
//Get Width * Height of Element
int divWidth = resize_div.getSize().getWidth();
int divHeight = resize_div.getSize().getHeight();
System.out.println("Actual width: " + divWidth);
//Get half of Height
int halfHeight = divHeight/2;
Actions builder = new Actions(driver);
Action resizable = builder
.moveToElement(resize_div, divWidth, halfHeight)
.clickAndHold()
.moveByOffset(divWidth, 0)
.release()
.build();
resizable.perform();
//Get Width of Element
divWidth = resize_div.getSize().getWidth();
System.out.println("Final width: " + divWidth);
}