2012-04-09 194 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(...);