如何使用javascript使用selenium Webdriver使用java设置所选Web元素的属性? [英] How to use javascript to set attribute of selected web element using selenium Webdriver using java?

查看:98
本文介绍了如何使用javascript使用selenium Webdriver使用java设置所选Web元素的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用javascript为网页上的所选元素设置属性。

I want to use javascript to set attribute for selected element on webpage.

我找到了两种使用javascript设置属性的方法

I have found 2 ways to set attribute using javascript

1

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

2

WebElement element = driver.findElement(By.id("foo"));
    String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element);

但我想将javascript应用于我使用selenium webdriver找到的特定webelement

But I want to apply javascript to specific webelement which i have found using selenium webdriver

作为示例,我使用selenium webdriver选择一个链接

as an example i have select one link using selenium webdriver

driver.findElement(By.linkText("Click ME"))

现在我想用javascript设置这个webelement的属性

Now I want to set attribute of this webelement using javascript

但我不知道如何将两者结合起来

but I don't know how to combine both

请帮我找到解决方案

推荐答案

以下行:

JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.linkText("Click ME"));
js.executeScript("arguments[0].setAttribute('attr', '10')",element);

这篇关于如何使用javascript使用selenium Webdriver使用java设置所选Web元素的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆