Reputation: 79
I'm using selenium webdriver in c# to automate filling input forms and using the SendKeys(); method, but it is taking a very long time to fill the input forms. I'm wondering if there's a way to fill all the input forms at once or if there's a faster way to fill them one at a time than the SendKeys(); method. I essentially want to fill the forms as fast as possible. I would appreciate any help.
My code example:
//name
chromeDriver.FindElementByXPath("//*[@id='order_billing_name']").SendKeys("John Doe");
//email
chromeDriver.FindElementByXPath("//*[@id='order_email']").SendKeys("[email protected]");
//telephone
chromeDriver.FindElementByXPath("//*[@id='order_tel']").SendKeys("123-456-7890");
Upvotes: 1
Views: 4497
Reputation: 448
As kodingkuma told you, a faster way it's to use JavaScriptExecutor But it depend also from the structure of the webPage. I think a good approach could be: Search on 'google' whats the fastest way to find WebElements (googling 'selenium fastest way find element' you will find dozens of examples).
I.E. some results:
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
And then, build some different Procedures and measure the time needed to be loaded.
(In my opinion a good mode to find the elements it's initially check if the elements that you need are listed ('li') in a list ('ul' or 'ol'), and if is possible, instantiate a list(Of IWebElements); and then loop every WebElement inside it)
Here an example:
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
P.S. Note that to use javaScriptExecutor, you need to use Javascript Syntax, between '...'.
Upvotes: 0
Reputation: 8506
I guess your screen has too many elements. That can slow down the search algorithm.
How about you first find the form of the fields that you want to send keys, and then you search for the elements within the form? In that way, you discard all other elements outside the form, so the performance should be increased.
Something like this:
WebElement form = chromeDriver.FindElementById("my-form")
//name
form.FindElementByXPath("//*[@id='order_billing_name']").SendKeys("John Doe");
//email
form.FindElementByXPath("//*[@id='order_email']").SendKeys("[email protected]");
//telephone
form.FindElementByXPath("//*[@id='order_tel']").SendKeys("123-456-7890");
Upvotes: 0
Reputation: 433
If you are just web-scraping data you could just execute javascript to do that and it should be the quickest in theory. Something like this should work.
JavascriptExecutor js = (JavascriptExecutor)chromeDriver;
js.executeScript(@"document.getElementById('order_billing_name').value = 'John Doe';");
If you are actually using this for testing you could save some time using the "find_element_by_id" call instead of the "find_element_by_xpath" call using the id. This could be a lot faster depending on the browser. So either of these would work.
chromeDriver.FindElement(By.Id("order_billing_name")).SendKeys("John Doe");
chromeDriver.FindElementById("order_billing_name").SendKeys("John Doe");
Upvotes: 3