【问题标题】:Using execute_async_script in the Selenium WebDriver在 Selenium WebDriver 中使用 execute_async_script
【发布时间】:2012-04-22 05:56:18
【问题描述】:

我想使用 execute_async_script 命令(在 Selenium 远程 webdriver 中)执行一些带有回调的 JS。

在我当前的 selenium.selenium 模型设置中,我有类似的东西:

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

但是我如何将 WebDriver 实现与 selenium.selenium 一起使用,以便调用 execute_async_script?

【问题讨论】:

    标签: javascript python asynchronous selenium webdriver


    【解决方案1】:

    听起来您现在正在使用遥控器设置,是吗?您应该能够从该代码中实例化 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(...);
    

    【讨论】:

      猜你喜欢
      • 2019-09-06
      • 1970-01-01
      • 2013-09-10
      • 2016-07-27
      • 1970-01-01
      • 2021-07-12
      • 2012-01-04
      • 2013-12-31
      • 2016-01-17
      相关资源
      最近更新 更多