在C#中使用Selenium WebDriver执行JavaScript

4szc88ey  于 2022-11-24  发布在  C#
关注(0)|答案(9)|浏览(212)

这是如何实现的?这里说java版本是:

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

但是我找不到C#代码来完成这个任务。

vu8f3i0k

vu8f3i0k1#

NET语言绑定中的对象、方法和属性名称并不完全对应于Java绑定中的名称。该项目的原则之一是,每种语言绑定都应该让那些熟悉该语言的编程人员“感觉自然”。在C#中,您希望执行JavaScript的代码如下所示

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

请注意,WebDriver API for .NET的完整文档位于this link

avkwfej4

avkwfej42#

我更喜欢使用扩展方法来获取scripts对象:

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

用作:

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

ygya80vv3#

nuget包Selenium.Support已经包含了一个扩展方法来帮助完成这个任务。2一旦包含了它,一个执行脚本的liner

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

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

fykwrbwg4#

@MortenChristiansen不错的扩展方法思想的一个稍微简化的版本怎么样:

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

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

或者是通用版本:

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");
dfuffjeb

dfuffjeb5#

您还可以执行以下操作:

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);
}
7eumitmz

7eumitmz6#

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);

    }
x9ybnkn6

x9ybnkn67#

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

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

在代码中,您可以执行以下操作:

IWebDriver driver = new WhateverDriver();
string test = driver.ExecuteJavaScript<string>(" return 'hello World'; ");
int test = driver.ExecuteJavaScript<int>(" return 3; ");
tzcvj98z

tzcvj98z8#

最短代码

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);");
guz6ccqo

guz6ccqo9#

请使用下面添加的扩展方法来执行javascript并在Selenium中截图。Selenium C#的支持(.dll)
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");

相关问题