Powershell with Selenium: Error: element not interactable

Tags: , ,



I need to update a textarea with a value. The code below throws an error “element not interactable”. This is because the textarea has “display:none”. If I remove the word NONE manually and then run the script again, it works great and is able to set the value of the textarea.

$browser = Start-SeChrome
$url = "https://www.freepik.com/profile/login"
$browser.Navigate().GoToURL($url)

$CaptchaResponse = "03AGdBq27yHAQ62QjKrtg"
ForEach ($TextArea_Element in (Find-SeElement -Driver $browser -TagName TextArea))
   {
   if ($TextArea_Element.GetAttribute('id') -eq "g-recaptcha-response") {$TextArea_Element.SendKeys($CaptchaResponse)}   
   Break
   }   

So the only option available is to use Javascript so that I can interact with the DOM directly (https://fijiaaron.wordpress.com/2018/01/29/how-to-access-elements-when-you-get-elementnotinteractableexception/). So I would have to do something like this:

$browser.executeScript("document.getElementById('g-recaptcha-response').value = $CaptchaResponse")
$browser.executeScript("___grecaptcha_cfg.clients[0].L.L.callback($CaptchaResponse)")

But now I get a different set of errors: javascript error: Invalid or unexpected token

Answer

Try to edit your code as below to execute js with variable:

$browser.executeScript("document.getElementById('g-recaptcha-response').value = arguments[0];", $CaptchaResponse)

As same as first line, you should extract the variable and replace to arguments[0]

$browser.executeScript("___grecaptcha_cfg.clients[0].L.L.callback('arguments[0]');", $CaptchaResponse)

You can execute JS like this, use arguments to pass your variables into JavaScript:

$new_style = "display: block; left: 20px;"
$browser.executeScript("arguments[0].style='arguments[1]';", $element, $new_style)


Source: stackoverflow