简体   繁体   English

在C#中使用Selenium WebDriver执行JavaScript

[英]Execute JavaScript using Selenium WebDriver in C#

How is this achieved?这是如何实现的? Here it says the java version is: 这里说 java 版本是:

WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("return document.title");

But I can't find the C# code to do this.但我找不到 C# 代码来执行此操作。

The object, method, and property names in the .NET language bindings do not exactly correspond to those in the Java bindings. .NET 语言绑定中的 object、方法和属性名称与 Java 绑定中的名称不完全对应。 One of the principles of the project is that each language binding should "feel natural" to those comfortable coding in that language.该项目的原则之一是,每种语言绑定对于那些使用该语言进行舒适编码的人来说应该“感觉自然”。 In C#, the code you'd want for executing JavaScript is as follows在 C# 中,执行 JavaScript 所需的代码如下

IWebDriver driver; // assume assigned elsewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("return document.title");

Note that the complete documentation of the WebDriver API for .NET can be found atthis link .请注意,可以在此链接中找到用于 .NET 的 WebDriver API 的完整文档。

I prefer to use an extension method to get the scripts object:我更喜欢使用扩展方法来获取脚本 object:

public static IJavaScriptExecutor Scripts(this IWebDriver driver)
{
    return (IJavaScriptExecutor)driver;
}

Used as this:用作:

driver.Scripts().ExecuteScript("some script");

the nuget package Selenium.Support already contains an extension method to help with this. nuget package Selenium.Support已经包含一个扩展方法来帮助解决这个问题。 Once it is included, one liner to executer script包含后,执行器脚本的一个衬里

  Driver.ExecuteJavaScript("console.clear()");

or或者

  string result = Driver.ExecuteJavaScript<string>("console.clear()");

How about a slightly simplified version of @Morten Christiansen's nice extension method idea: @Morten Christiansen 的不错的扩展方法想法的稍微简化的版本怎么样:

public static object Execute(this IWebDriver driver, string script)
{
    return ((IJavaScriptExecutor)driver).ExecuteScript(script);
}

// usage
var title = (string)driver.Execute("return document.title");

or maybe the generic version:或者也许是通用版本:

public static T Execute<T>(this IWebDriver driver, string script)
{
    return (T)((IJavaScriptExecutor)driver).ExecuteScript(script);
}

// usage
var title = driver.Execute<string>("return document.title");

You could also do:你也可以这样做:

public static IWebElement FindElementByJs(this IWebDriver driver, string jsCommand)
{
    return (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(jsCommand);
}

public static IWebElement FindElementByJsWithWait(this IWebDriver driver, string jsCommand, int timeoutInSeconds)
{
    if (timeoutInSeconds > 0)
    {
        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
        wait.Until(d => d.FindElementByJs(jsCommand));
    }
    return driver.FindElementByJs(jsCommand);
}

public static IWebElement FindElementByJsWithWait(this IWebDriver driver, string jsCommand)
{
    return FindElementByJsWithWait(driver, jsCommand, s_PageWaitSeconds);
}
public void javascriptclick(String element)
    { 
        WebElement webElement=driver.findElement(By.xpath(element));
        JavascriptExecutor js = (JavascriptExecutor) driver;

        js.executeScript("arguments[0].click();",webElement);   
        System.out.println("javascriptclick"+" "+ element);

    }
public static class Webdriver
{        
    public static void ExecuteJavaScript(string scripts)
    {
        IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
        js.ExecuteScript(scripts);
    }

    public static T ExecuteJavaScript<T>(string scripts)
    {
        IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
        return (T)js.ExecuteScript(scripts);
    }
}

In your code you can then do在您的代码中,您可以执行

string test = Webdriver.ExecuteJavaScript<string>(" return 'hello World'; ");
int test = Webdriver.ExecuteJavaScript<int>(" return 3; ");

Please use the below extension methods added to execute javascript and to take screenshot in Selenium.Support (.dll) of Selenium C#请使用以下添加的扩展方法执行javascript并在Selenium中进行截图。Support (.dll) of Selenium C#

https://www.nuget.org/packages/Selenium.Support/ https://www.nuget.org/packages/Selenium.Support/

IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
driver.Url = "https://phptravels.net/";

driver.ExecuteJavaScript("document.querySelector('#checkin').value='07-12-2022'");

driver.ExecuteJavaScript("document.querySelector('#checkout').value='17-12-2022'");

IWebElement ele1 = driver.FindElement(By.Id("checkin"));
driver.ExecuteJavaScript("arguments[0].value='07-12-2022'",ele1);

string output=driver.ExecuteJavaScript<string>("return 
document.querySelector('#checkin').value");
Console.WriteLine(output);

Screenshot sc= driver.TakeScreenshot();
sc.SaveAsFile("C:\\error.png");

The shortest code最短的代码

ChromeDriver drv = new ChromeDriver();

drv.Navigate().GoToUrl("https://stackoverflow.com/questions/6229769/execute-javascript-using-selenium-webdriver-in-c-sharp");

drv.ExecuteScript("return alert(document.title);");


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM