本文介绍了Selenium WebDriver中的JavaScript执行器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在脚本中使用JavaScript.
I want to use JavaScript for my script.
我创建了JavaScriptExecutor
的object
,但是没有executeScript()
方法.当我使用executeScript()
方法时,它显示错误.
I have created an object
of JavaScriptExecutor
, but executeScript()
method is not present. It shows error when I use executeScript()
method.
这是我使用的代码:
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;
public class GetDomain_JS {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
driver.manage().window().maximize();
System.out.println(driver.getCurrentUrl());
JavaScriptExecutor js=(JavaScriptExecutor) driver;
String domain_name=(String) js.executeScript("return document.domain");
System.out.println(doamin_name);
}
}
推荐答案
它对我有用;您在JavaScriptExecutor
上输入大写字母S
时出错.相反,您应该使用小写字母为s
的javascriptExecutor
.
It works for me; you had a mistake on JavaScriptExecutor
with upper case S
. Instead, you should have javascriptExecutor
with lower case s
.
尝试以下代码:
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class GetDomain_JS {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
driver.manage().window().maximize();
System.out.println(driver.getCurrentUrl());
JavascriptExecutor js=(JavascriptExecutor) driver;
String domain_name=(String) js.executeScript("return document.domain");
System.out.println(domain_name);
}
}
这对我有用!!如果适合您,请重击!
This works for me!! Please thumps up if it does for you!
这篇关于Selenium WebDriver中的JavaScript执行器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!