简体   繁体   English

无法在 PowerShell 中触发点击事件

[英]Unable to trigger click event in PowerShell

I want to automate a task at my work where i need to fill a form in one of our intranet sites.我想在我的工作中自动化一项任务,我需要在我们的一个 Intranet 站点中填写表格。 Basically i need to call the site, fill a specific textbox and click the save button.基本上我需要调用该站点,填写一个特定的文本框,然后单击保存按钮。 I am trying first to test the code on Google website but it is not working, although i am copying most of it from various sources.我首先尝试在 Google 网站上测试代码,但它不起作用,尽管我从各种来源复制了大部分代码。

The code mostly works fine.该代码大部分工作正常。 I am able to open Internet explorer, navigate to google, fill the search textbox, but i am stuck when clicking the button.我可以打开 Internet Explorer,导航到谷歌,填写搜索文本框,但是单击按钮时我被卡住了。

$Url

$Textbox


$Url = “www.google.com” 

$Textbox=”test” 

$IE = New-Object -com internetexplorer.application; 

$IE.visible = $true 

$IE.navigate($url) 

while ($IE.Busy -eq $true) 

 { 

 Start-Sleep -Milliseconds 2000 

 } 

$IE.Document.getElementsByname(“q”)[0].value = $Textbox 

$IE.Document.getElementsByname(“btnk”)[0].Click()

while ($IE.Busy -eq $true) 

{ 

Start-Sleep -Milliseconds 2000 

}

The error message i keep getting is this:我不断收到的错误消息是:

You cannot call a method on a null-valued expression.您不能在空值表达式上调用方法。 At line:17 char:1 + $IE.Document.getElementsByname(“btnk”)[0].Click() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId: InvokeMethodOnNull在 line:17 char:1 + $IE.Document.getElementsByname(“btnk”)[0].Click() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId: InvokeMethodOnNull

enter image description here在此处输入图像描述

getElementsByname name parameter is case sensitive. getElementsByname name参数区分大小写。

The name of the Search element is btnK and not btnk . Search 元素的名称是btnK而不是btnk

Change to:改成:

$IE.Document.getElementsByname("btnK")[0].Click()

I've always used:我一直用:

$Button_Name.RaiseEvent((New-Object -TypeName System.Windows.RoutedEventArgs $([System.Windows.Controls.Button]::ClickEvent)))

Because it works for me and I'm too lazy to look for anything else.因为它对我有用,我懒得去寻找其他东西。

I have reproduced the problem on my machine, it seems that this behavior is related to the IE browser protected mode.我已经在我的机器上重现了这个问题,这个行为似乎与 IE 浏览器保护模式有关。 Before turning on the protected mode, it will show this behavior.在打开保护模式之前,它会显示此行为。

To solve this issue, please turn on the Protected Mode (in my machine, I enabled the protected mode in different zones. The screenshot like this ).要解决这个问题,请打开保护模式(在我的机器上,我在不同的区域启用了保护模式。截图是这样的)。

Besides, you could also run the PowerShell as admin.此外,您还可以以管理员身份运行 PowerShell。

The PowerShell script as below: PowerShell 脚本如下:

$Url
$Textbox
$Url = "www.google.com" 
$Textbox="test" 
$IE = New-Object -com internetexplorer.application; 
$IE.visible = $true 
$IE.navigate($url) 
while ($IE.Busy -eq $true) { Start-Sleep -Milliseconds 2000  } 
Start-Sleep -Seconds 1
$IE.Document.getElementsByName("q")[0].value = $Textbox
Start-Sleep -Seconds 1
$IE.Document.getElementsByName("btnK")[0].Click()
while ($IE.Busy -eq $true) { Start-Sleep -Milliseconds 2000 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM