我是自动化测试的新手。在我的代码中,我在Selenium自动化测试脚本(使用JUnit)上使用了JavaScript。我正在尝试查找当前窗口标题:
driver = new ChromeDriver();
js = (JavaScriptExecutor)driver;
driver.get(//passed url);
//after the log in my Org. I execute the command below
//to get the title of the current window I execute the below command
js.executeScript("document.getElementsByTagName('title').innerHTML;");
,但上面的命令将返回作为获取窗口标题的空瞬间的答案。有人知道我的命令出了什么问题吗?
但我在页面的控制台日志上获得了执行时窗口的标题。所以我不知道我的代码出了什么问题。
谢谢。
莫汉·拉杰·S。
发布于 2018-01-02 18:57:31
更改以下行:
document.getElementsByTagName('title').innerHTML
至:
返回document.getElementsByTagName('title').innerHTML
在代码中,它将是:
js.executeScript("return document.getElementsByTagName('title')[0].innerHTML;");
getElementsByTagName返回元素数组。所以我们需要传递索引值。
Web驱动程序接口提供get title方法。这很简单。
String title=driver.getTitle();
System.out.println("Title is" + title);
发布于 2018-10-12 05:07:18
你也可以这样做
function titleCheck() {
promise = driver.getTitle();
promise.then(function(title) {
console.log("The title is: " + title);
});
}
参考:https://blog.testproject.io/2018/03/08/selenium-javascript-best-practices/
虽然他们的var测试内容对我不起作用,但我必须删除它。
发布于 2020-07-01 22:59:21
JavascriptExecutor js = (JavascriptExecutor)driver;
String text = js.executeScript("return document.title;").toString();
https://stackoverflow.com/questions/48059614
复制相似问题