如何通过Selenium v​​b.net/c#中的JavascriptExecutor注入javascript事件监听器?

时间:2018-08-26 10:09:48

标签: javascript c# vb.net selenium

在我的vb.net/c#硒交互代码中,我需要通过javaScriptExecutor类注入一个javascript事件监听器。

此链接说明了如何使用javascript

Can my WebDriver script catch a event from the webpage?

我不知道JavaScript语法。

我需要翻译vb.net(也可以使用c#)。 这应该是代码:

Private Sub test(ByVal driver As OpenQA.Selenium.IWebDriver)
    'create instance of an element
    Dim MyWebElement As OpenQA.Selenium.IWebElement
    MyWebElement = driver.FindElement(OpenQA.Selenium.By.CssSelector("..."))

    ' add an event listener on an element
    Dim js As OpenQA.Selenium.IJavaScriptExecutor = CType(driver, OpenQA.Selenium.IJavaScriptExecutor)
    js.ExecuteScript("...here the javascript code for inject the event listener...", MyWebElement)

    ' upload the file
    js.ExecuteAsyncScript("...same here...", MyWebElement)

End Sub

这是我无法翻译的说明:

# add an event listener on an element
driver.execute_script("""\
arguments[0].addEventListener("change", function onchange() {
this.removeEventListener("change", onchange);
window.__file__ = true;
});
window.__file__ = false;
""", input_elem)

0 个答案:

没有答案