微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 在Selenium WebDriver中使用execute_async_script

我想使用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(...);

原文地址:https://codeday.me/bug/20190626/1291098.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐