[英]Powershell with Selenium: Error: element not interactable
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".这是因为 textarea 有“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.如果我手动删除单词 NONE 然后再次运行脚本,它会很好地工作并且能够设置 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/ ).所以唯一可用的选择是使用 Javascript 以便我可以直接与 DOM 交互( 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但现在我得到了一组不同的错误:javascript 错误:无效或意外的令牌
Try to edit your code as below to execute js with variable:尝试如下编辑您的代码以使用变量执行 js:
$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]与第一行一样,您应该提取变量并替换为 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:您可以像这样执行 JS,使用arguments
将您的变量传递给 JavaScript:
$new_style = "display: block; left: 20px;"
$browser.executeScript("arguments[0].style='arguments[1]';", $element, $new_style)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.