【问题标题】:Is There Faster Way to Fill Input Forms With Selenium C#是否有更快的方法来使用 Selenium C# 填充输入表单
【发布时间】:2018-07-01 18:09:36
【问题描述】:

我在 c# 中使用 selenium webdriver 来自动填写输入表单并使用 SendKeys();方法,但是填写输入表单需要很长时间。我想知道是否有一种方法可以一次填写所有输入表单,或者是否有比 SendKeys(); 一次填写一个更快的方法。方法。我基本上想尽快填写表格。我将不胜感激。

我的代码示例:

//name
chromeDriver.FindElementByXPath("//*[@id='order_billing_name']").SendKeys("John Doe");

//email
chromeDriver.FindElementByXPath("//*[@id='order_email']").SendKeys("Johndoe@gmail.com");

//telephone
chromeDriver.FindElementByXPath("//*[@id='order_tel']").SendKeys("123-456-7890");

【问题讨论】:

    标签: c# selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    如果您只是网络抓取数据,您可以执行 javascript 来执行此操作,理论上它应该是最快的。这样的事情应该可以工作。

    JavascriptExecutor js = (JavascriptExecutor)chromeDriver;   
    
    js.executeScript(@"document.getElementById('order_billing_name').value = 'John Doe';");
    

    如果您实际使用它进行测试,您可以使用“find_element_by_id”调用而不是使用 id 的“find_element_by_xpath”调用来节省一些时间。这可能会快很多,具体取决于浏览器。所以这两种方法都可以。

    chromeDriver.FindElement(By.Id("order_billing_name")).SendKeys("John Doe");
    chromeDriver.FindElementById("order_billing_name").SendKeys("John Doe");
    

    【讨论】:

    • 非常感谢您的回复!我会尝试它们并分享测试结果。
    • JavascriptExecutor 选项非常适合在测试设置中将长 JSON 值输入到 TextArea 中 - 每次都需要几秒钟,现在是亚秒级。
    【解决方案2】:

    我猜你的屏幕元素太多了。这会减慢搜索算法的速度。

    先找到要发送键的字段的表单,然后搜索表单中的元素怎么样?这样一来,您将丢弃表单之外的所有其他元素,因此应该会提高性能。

    类似这样的:

        WebElement form = chromeDriver.FindElementById("my-form")
    
        //name
        form.FindElementByXPath("//*[@id='order_billing_name']").SendKeys("John Doe");
    
        //email
        form.FindElementByXPath("//*[@id='order_email']").SendKeys("Johndoe@gmail.com");
    
        //telephone
        form.FindElementByXPath("//*[@id='order_tel']").SendKeys("123-456-7890");
    

    【讨论】:

    • 谢谢,我一定会试试的!
    【解决方案3】:

    正如kodingkuma 告诉你的那样,使用 JavaScriptExecutor 是一种更快的方法 但它也取决于网页的结构。 我认为一个好的方法可能是: 在“google”上搜索找到 WebElements 的最快方法是什么(谷歌搜索“selenium 最快的方法查找元素”,您会找到几十个示例)。

    I.E.一些结果:

    Which is the best and fastest way to find the element using webdriver? By.XPath or By.ID or anything else? And why? [closed]

    What is the fastest and slowest ways of finding elements using Selenium Webdriver?

    Which is the Best and Fastest Way to Find Elements Using Selenium WebDriver

    然后,构建一些不同的程序并测量需要加载的时间。

    (在我看来,查找元素的一种很好的模式是首先检查您需要的元素是否在列表('ul' 或 'ol')中列出('li'),如果可能,实例化一个list(Of IWebElements); 然后循环其中的每个 WebElement)

    这里是一个例子:

    Dim jsExec As OpenQA.Selenium.IJavaScriptExecutor
    jsExec = CType(driver, OpenQA.Selenium.IJavaScriptExecutor)
    Dim sw As New Stopwatch
    Dim MyListOfWebElements As System.Collections.ObjectModel.ReadOnlyCollection(Of IWebElement)
    
    Public Sub Selenium_Load_WebElements_By_JsExecutor()
        sw.Restart()
        MyListOfWebElements = jsExec.ExecuteScript("var result = document.querySelector('...here you put your css selector...'); if(result === null) {} else {result = result.querySelectorAll('li')}; return result;")
        sw.Stop()
        MsgBox("WebElement List (jsExec-css) - Loading time (ms): " & sw.ElapsedMilliseconds)
    End Sub
    
    Public Sub Selenium_Load_WebElements_By_Css()
        sw.Restart()
        MyListOfWebElements = Driver.driver.FindElements(By.CssSelector("...your css selector...")).ToList
        sw.Stop()
        MsgBox("WebElement List (Css) - Loading time (ms): " & sw.ElapsedMilliseconds)
    End Sub
    
    Public Sub Selenium_Load_WebElements_By_Id()
        sw.Restart()
        MyListOfWebElements = Driver.driver.FindElements(By.Id("...your id...")).ToList
        sw.Stop()
        MsgBox("WebElement List (Id) - Loading time (ms): " & sw.ElapsedMilliseconds)
    End Sub
    

    附:请注意,要使用 javaScriptExecutor,您需要在 '...' 之间使用 Javascript 语法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2019-10-06
      • 2022-11-20
      • 2015-05-08
      • 2017-02-20
      相关资源
      最近更新 更多