2012-04-09 146 views
4

我想使用execute_async_script命令(在Selenium远程webdriver中)通过回调执行一些JS。在Selenium WebDriver中使用execute_async_script

我有我的当前设置为selenium.selenium模式与此类似:

self.selenium = selenium("localhost", 4444, "*firefox", "http://localhost:8000") 

但我怎么使用webdriver的执行一起selenium.selenium所以我可以调用execute_async_script?

回答

4

这听起来像你现在正在使用遥控器设置,是吗?您应该能够从该代码中实例化WebDriver实例,但您需要引用WebDriver dll。您需要实例化浏览器驱动程序对象的实例(即:FirefoxDriver,InternetExplorerDriver,ChromeDriver等),然后将您的IWebDriver“驱动程序”属性设置为与该实例相同。然后创建一个名为“js”(或任何你想要的)的接口对象作为IJavaScriptExecutor对象,并调用非静态方法“ExecuteScript”或“ExecuteAsyncScript”(在你的情况下)。

我的代码在C#.NET中(假设你使用的是NUnit)。因为我不知道这种语言,所以你必须找到Python的实现。

类的数据成员:

private IWebDriver driver; 
private StringBuilder verificationErrors; 
private string baseURL; 

代码:

driver = new FirefoxDriver(new FirefoxProfile()); 
baseURL = "http://???"; // replace "???" with website domain 
ISelenium selenium = new WebDriverBackedSelenium(driver, baseURL); 
selenium.Start(); 

IJavaScriptExecutor js = driver as IJavaScriptExecutor; 
js.ExecuteScript("$('#id').click();"); // assumes JQuery is used in page 
js.ExecuteAsyncScript(...);