无法在Javascript功能启用按钮上执行Selenium WebDriver命令
Unable to execute Selenium WebDriver command on Javascript function enabled button
我指的是http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick_copy。在单击按钮时,应该将field1中的值复制到field2中。我尝试用selenium webdriver自动化它。我使用WebDriver
命令为:
driver.get("http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick_copy");
driver.findElement(By.xpath(".//button[@onclick='myFunction()']")).click();
但是我得到了错误:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//button[@onclick='myFunction()']"}
Command duration or timeout: 20.11 seconds
之后我又试了:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('"field2'").value = document.getElementById('"field1'").value;");
这一次我得到了错误:
org.openqa.selenium.WebDriverException: document.getElementById(...) is null
按钮位于iframe内。在找到元素之前,您必须切换到框架。参见下面的代码:
driver.get("http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick_copy");
driver.switchTo().frame(driver.findElement(By.id("iframeResult")));
driver.findElement(By.xpath(".//button[@onclick='myFunction()']")).click();
相关文章:
- 在selenium python中执行脚本
- 适用于多种浏览器的Selenium-需要能够执行javascript的webDriver
- 执行JavaScript代码,使用selenium webdriver或WatiN从控制台获取日志
- 在Selenium IDE中执行HTTP POST
- 当JS在Chrome控制台上正常工作时,Selenium JavaScript执行器返回null
- 在python中使用Selenium执行Javascript(Tkinter作为GUI)
- 如何创建javascript执行器,使元素在selenium webdriver中可见
- 在 Python 中使用 Selenium 执行 JavaScript 后存储结果
- 了解在Selenium中执行异步脚本
- 如何在Selenium IDE中对数组中的任何值执行断言
- 阻止 Javascript 执行阻止 selenium webdriver
- 使用 selenium webdriver 执行外部.js文件
- 在 Node JavaScript 中使用 Selenium-Webdriver 执行“命令”
- Selenium WD NullPointerException 同时执行简单的 JS 代码
- 在Selenium WebDriver中执行“.hover”Javascript
- Selenium测试在执行javascript时随机崩溃
- 通过Selenium Webdriver中的Java脚本执行器传递变量
- Selenium Javascript执行器返回null
- 如何在selenium中执行js
- 无法在Javascript功能启用按钮上执行Selenium WebDriver命令