我想使用execute_async_script命令(在Selenium远程webdriver中)通过回调执行一些JS.
在我目前的selenium.selenium模型设置中,我有类似的东西:
self.selenium = selenium("localhost", 4444, "*firefox", "http://localhost:8000")
但是我如何使用WebDriver实现和selenium.selenium一起使用,所以我可以调用execute_async_script?
解决方法:
听起来你现在正在使用遥控器设置,是吗?您应该能够在该代码中实例化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(...);