2014-02-21 65 views
0

这是一个触发Internet Explorer的PowerShell脚本,打开LinkedIn登录页面并在用户名文本字段中输入一些文本。如何通过Power Shell将文本输入到输入字段中?

$ie = New-Object -Com "InternetExplorer.Application" 
$ie.Navigate("www.linkedIn.com") 
$ie.Visible = $true 

$doc = $ie.document 
$usernameElement = $doc.getElementByTagName("input") | Where-Object {$_.id = "session_key-login""} 
$usernameElement.Click() 

Get-Process iexplore | Foreach-Object {$_.CloseMainWindow()} 

不幸的是,我不断收到以下错误:

You cannot call a method on a null-valued expression. 
At C:\Users\Pinku\Desktop\Untitled1.ps1:7 char:23 
+ $usernameElement.Click <<<<() 
+ CategoryInfo   : InvalidOperation: (Click:String) [], RuntimeExcepti 
on 
+ FullyQualifiedErrorId : InvokeMethodOnNull 

我都试过,但一直无法从这个issue.Please建议减轻喽!

回答

0

而不是使用$doc.getElementsByTagName("input"),然后试图通过结果来过滤,请尝试直接检索ID使用getElementById

$usernameElement = $doc.getElementById("session_key-login") 
$usernameElement.Click() 

---编辑---

响应仍然得到null-valued expression后使用上述:

错误消息是它无法找到所谓的“session_key可以登录”的任何元素,因此它返回$空,因此,当您尝试调用Click()方法,它会抛出错误。有些事情可以尝试:

- 查看id是否存在。创建$即对象后,运行下面的代码,并查看是否有匹配“session_key可以登录”的ID:

$ie = New-Object -Com "InternetExplorer.Application" 
$ie.Navigate("www.linkedIn.com") 
$ie.Visible = $true 

$doc = $ie.document 
$doc.getElementsByTagName("Input") | Select Id, Name 

- 尝试运行你的PowerShell会话为管理员。我知道我无法正确启动IE,直到我以管理员身份运行PowerShell。例如。即使创建了iexplore进程,物理Internet Explorer窗口也不会为我打开。

+0

嗨,感谢您的回复,但我已经尝试过您的解决方案。我仍然得到同样的错误。 –

+0

请参阅上述编辑以获取其他疑难解答步骤 – HAL9256

+0

我收到此贴子您的建议 - '您无法在空值表达式上调用方法。 在C:\ Users \ Pinku \ Desktop \ Untitled1.ps1:6 char:45 + $ usernameElement = $ doc.getElementsByTagName <<<<(“Input”)|选择ID,名称 + CategoryInfo:InvalidOperation:(getElementsByTagName:String)[],RuntimeException + FullyQualifiedErrorId:InvokeMethodOnNull 您无法在空值表达式上调用方法。 在C:\用户\产品库\桌面\ Untitled1.ps1:7字符:23 + $ usernameElement.Click <<<<() + CategoryInfo:InvalidOperation:(点击:字符串)[],的RuntimeException + FullyQualifiedErrorId: InvokeMethodOnNull' –