這聽起來像你現在正在使用遙控器設置,是嗎?您應該能夠從該代碼中實例化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(...);