Selenium:我可以在 Selenium 中设置 WebElement 的任何属性值吗?

Posted

技术标签:

【中文标题】Selenium:我可以在 Selenium 中设置 WebElement 的任何属性值吗?【英文标题】:Selenium: Can I set any of the attribute value of a WebElement in Selenium? 【发布时间】:2012-01-18 08:53:50 【问题描述】:

我有一个 WebElement,我想将其属性值重置为其他值(例如,attr 是属性,我想将其原始 value=1 更改为新的 value=10)。

有可能吗?我正在使用 Selenium 2.0 (WebDriver.)

【问题讨论】:

【参考方案1】:

您必须使用 javascriptExecutor 类:

WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('//id of element').setAttribute('attr', '10')");

【讨论】:

您好,感谢您的回复,但在 Sel2.0 中我们无法获取文档对象引用。我们必须通过 WebDriver 获取元素,它返回不支持属性设置的“WebElement”。 driver.findElement(By.id("元素的id')) @yami 在 Selenium 2 中,您可以执行支持访问 DOM 的 javascript。就像在网站上运行相同的代码一样。您只是不使用 WebElement 类,而是使用 JavascriptExecutor 类并使用 WebDriver 进行转换。 没错。我再次验证,我们可以得到完整的 DOM 引用并对其进行操作。然而,一旦我们将 JS 脚本作为字符串传递给 executeScript,调试就会变得很困难。虽然我觉得如果它可以通过 WebElement 并且它应该为我们提供 DOM 的完整功能,那么它会很棒。但是,无论这个答案对我有多大帮助..:-)到目前为止,我都可以接受。谢谢..!! 快速问题:有没有办法在 web 元素的上下文中执行 javascript?换句话说:有一个WebElement(例如:一个没有其他属性的简单<span>)有办法执行javascript并以某种方式传递对这个js元素的引用。 (也许 js 中的this 会引用它?) 找到答案:将WebElement(s) 传递给执行者并在脚本中访问arguments[0]([1,2...])【参考方案2】:

如果您正在使用PageFactory 模式或已经引用了您的WebElement,那么您可能希望使用对WebElement 的现有引用来设置属性。 (而不是在你的 javascript 中使用document.getElementById(...)

以下示例允许您使用现有的 WebElement 参考设置属性。

代码片段

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.FindBy;

public class QuickTest 

    RemoteWebDriver driver;

    @FindBy(id = "foo")
    private WebElement username;

    public void exampleUsage(RemoteWebDriver driver) 
        setAttribute(username, "attr", "10");
        setAttribute(username, "value", "bar");
    

    public void setAttribute(WebElement element, String attName, String attValue) 
        driver.executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);", 
                element, attName, attValue);
    

【讨论】:

我使用这种方法。它在 firefox v30 和 google chrome 和 IE 上成功,但在 firefox v35 上失败,它说 arguments[0] 是未定义的,尽管我已经等到这个元素存在了。 多次使用此方法与执行设置所有需要属性的单个脚本相比,性能如何? executeScript 方法的开销是不是很大? @mrog,我没有性能指标...(这是大约 3 年前)。还有,你拿什么来比较?它需要一个 var arg 参数数组,所以如果你想要“一个”调用,你可以同时设置多个属性......例如driver.executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);arguments[0].setAttribute(arguments[3], arguments[4]);", element, att1Name, att1Value, att2Name, att2Value);【参考方案3】:

基于先前答案的花哨 C# 扩展方法:

public static IWebElement SetAttribute(this IWebElement element, string name, string value)

    var driver = ((IWrapsDriver)element).WrappedDriver;
    var jsExecutor = (IJavaScriptExecutor)driver;
    jsExecutor.ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2]);", element, name, value);

    return element;

用法:

driver.FindElement(By.Id("some_option")).SetAttribute("selected", "selected");

【讨论】:

【参考方案4】:

另一个回答这个问题的人可以在这里找到@nilesh https://***.com/a/19934852/2079692

public void setAttributeValue(WebElement elem, String value)
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])",
        elem, "value", value
    );

这利用了 selenium findElementBy 函数,其中 xpath 也可以使用。

【讨论】:

IMO“值”是属性而不是属性。 '(arguments[0])[arguments[1]] = arguments[2]' 将适用于 value【参考方案5】:

我创建了这个 jquery 来解决我的问题。

public void ChangeClassIntoSelected(String name,String div) 
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("Array.from($(\"div." + div +" ul[name=" + name + "]\")[0].children).forEach((element, index) => \n" +
                "   $(element).addClass('ui-selected');\n" +
                ");");
    

使用此脚本,您可以将实际的类名更改为其他名称。

【讨论】:

欢迎来到 SO。请格式化您的代码并解释解决方案。在这里,仅仅提供解决方案是不够的。【参考方案6】:

我已经针对同样的问题发布了类似的解决方案,

访问How to use javascript to set attribute of selected web element using selenium Webdriver using java?

这里首先我们在我的例子中找到了元素我使用 xpath 找到了元素然后我们遍历元素列表然后我们将驱动程序对象转换为 Executor 对象并在这里创建一个脚本第一个参数是元素和第二个参数是属性,第三个参数是新值

List<WebElement> unselectableDiv = driver
                .findElements(By.xpath("//div[@class='x-grid3-cell-inner x-grid3-col-6']"));

        for (WebElement element : unselectableDiv) 

            // System.out.println( "**** Checking the size of div "+unselectableDiv.size());

            JavascriptExecutor js = (JavascriptExecutor) driver;

            String scriptSetAttr = "arguments[0].setAttribute(arguments[1],arguments[2])";

            js.executeScript(scriptSetAttr, element, "unselectable", "off");

            System.out.println(" *****   check value of Div property " + element.getAttribute("unselectable"));

        

【讨论】:

【参考方案7】:
    JavascriptExecutor js = (JavascriptExecutor) driver;        
    js.executeScript("document.getElementsByClassName('featured-heading')[0].setAttribute('style', 'background-color: green')");

我可以在java中使用上面的代码添加一个属性

【讨论】:

以上是关于Selenium:我可以在 Selenium 中设置 WebElement 的任何属性值吗?的主要内容,如果未能解决你的问题,请参考以下文章