简体   繁体   English

如何使用webdriver增加HTML表格列宽

[英]How to increase HTML table column width using webdriver

Currently I'm trying to automate a step involved to increase HTML table column width using Selenium webdriver. 目前我正在尝试使用Selenium webdriver自动执行增加HTML表格列宽的步骤。

Got to know that I can get the index of x and y coordinate with findElement(By.cssSelector("<Css locator>").getLocation(); 我知道我可以用findElement(By.cssSelector("<Css locator>").getLocation();获取x和y坐标的findElement(By.cssSelector("<Css locator>").getLocation();

But I'd like to know if it was possible to use the same concept to increase the width of a column. 但我想知道是否可以使用相同的概念来增加列的宽度。

You can use the Javascript Executor 您可以使用Javascript Executor

WebDriver driver;
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelector('<CSSLOCATOR>').setAttribute('width', <WIDTH>)");

where you fill in <CSSLOCATOR> and <WIDTH> with what you need. 在那里用你需要的东西填写<CSSLOCATOR><WIDTH>

Answer to my question is simple usage of jquery 回答我的问题是简单使用jquery

$(selector).width(value) ; $(selector).width(value);

Example usage String aScript = "jQuery(\\"" + CSS_TABLE_HEADER_BY_COL_NAME + "\\").width(%s)"; 示例用法String aScript =“jQuery(\\”“+ CSS_TABLE_HEADER_BY_COL_NAME +”\\“)。width(%s)”;

jse.executeScript(aScript );

Hope this should resolve most people of increasing column width in selenium. 希望这能解决大多数人在硒中增加色谱柱宽度的问题。

If its static table then we can add attirbute (not setAttribute, because we need to add new attributed 'width') 'width' with that tag. 如果是静态表,那么我们可以添加attirbute(不是setAttribute,因为我们需要添加新的属性'width')'width'和该标签。

String tableJS = "document.getElementsByTagName('table')[0].style.width='1000px'";
js.executeScript(tableJS);

Note: If cell OR table width is handled by external CSS then this may not work. 注意:如果单元格或表格宽度由外部CSS处理,那么这可能不起作用。

2nd Way:If we have resizble table, then we can find the element of 'resizer' and apply below solution by using Action class: 第二种方式:如果我们有可调整表,那么我们可以找到'resizer'的元素并使用Action类在下面的解决方案中应用:

if (resizeableElement.isDisplayed()) {
            Actions action = new Actions(driver);
            action.clickAndHold(resizeableElement).moveByOffset(100, 100).release().build().perform();
        } else {
            System.out.println("Element was not displayed to drag");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM