There may be times when you want to input strings into a form during browser processing using selenium operations in VBA.
In such cases, “SendKeys” is often used, but if the text is long, the processing will be considerably slower.
SendKeys is a method that simulates pressing keys one by one.
Even though it is a program, it takes a certain amount of time.
As an alternative, I will introduce a method to insert text into an element by running JavaScript on VBA.
This allows even thousands of characters to be processed in an instant.
Sub test() Dim driver As New Selenium.ChromeDriver 'Browser settings driver.Start "chrome" 'start chrome driver.Get "https://www.google.com/?hl=ja" 'Open by specifying URL Dim args(0) As Variant 'Array to pass to javascript (one this time) target_selector = "document.querySelector('#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input')" 'Specify the target element in javascript notation (getElementById or getElementsByClassName can also be used) args(0) = "text to insert" driver.ExecuteScript target_selector & ".value = arguments[0];", args 'run javascript driver.Close 'close chrome Set driver = Nothing 'reset drive End Sub
.ExecuteScript is a method to execute javascript on the browser via selenium on VBA.
Set the javascript to be executed in the first argument and the array to be passed to the javascript in the second argument.
The second argument is passed to “arguments[?]” in the javascript.
In this example, there is only one item, but when processing multiple items, it would be smarter to pass it as an array.
コメント