无法在Powershell中单击超链接

时间:2020-06-28 17:21:53

标签: javascript powershell

我无法使用Powershell单击链接。

<a class="abc" href="https://www.tutorialspoint.com/videotutorials/index.php"><i class="fa fa-video tp-banner-icons"></i> 
<span>Videos</span></a>

在Powershell中:

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Navigate("https://www.tutorialspoint.com/index.htm")
$search = $ie.Document.getElementsByTagName("a") | where-object {$_.className -eq "abc"}
$search.click()

我得到的输出如下:

System.__ComObject

在Internet Explorer的UI上什么也没有点击

2 个答案:

答案 0 :(得分:1)

跟进我的评论。

最好在旅行通过交互式路线之前先刮一下网站,看看有什么和不可用。

您必须先抓取该网站,才能看到Neko Musume暗示的Invoke_webRequest的可用功能。

# Define the target to scrape
$TP = Invoke-WebRequest -Uri 'https://www.tutorialspoint.com/videotutorials' -SessionVariable tp

# Review visible elements. I'm using OGV, but you can clip and paste to notepad
$TP.AllElements  | Out-GridView
$TP.Forms        | Out-GridView
$TP.Forms.Fields | Out-GridView
$TP.Links.href   | Out-GridView

了解了元素之后,只要可见即可使用浏览器进行交互导航并与之交互。

#Define you web target
$RequestURI = "https://www.tutorialspoint.com/videotutorials"

# Create your browser session
$IE         = New-Object -ComObject "InternetExplorer.Application"

# This must be set to true, execute methods on the site.
$IE.Visible = $true 
$IE.Silent  = $true
$IE.Navigate($RequestURI)

# Wait for IE to load
While ($IE.Busy) 
{ Start-Sleep -Seconds 1 }

这是我提供给他人的样本中的一个示例。请注意,这是较旧的代码,该站点可能已更改,因此,如果您尝试在那里进行测试,则它可能会或可能不再起作用。但是,概念是相同的。

$password   = '1234'
$loginUrl   = 'https://pwpush.com'

$ie         = New-Object -com internetexplorer.application
$ie.visible = $true
$ie.navigate($loginUrl)

while ($ie.Busy -eq $true) 
{ Start-Sleep -Seconds 1 }

($ie.document.getElementById('password_payload') | 
select -first 1).value = $password
Start-Sleep -Seconds 1 

$ie.Document.getElementsByName('commit').Item().Click();
Start-Sleep -Seconds 1 

因此,请尝试逐步浏览该网站(类似于以下内容),以针对您的需求。同样,只是一些想法供您尝试。

### Navigating web sites interactively

# Target URL
$url = 'https://www.tutorialspoint.com/videotutorials'


# Get form elements
$FormElements = Invoke-WebRequest -Uri $url -SessionVariable fe

# List all form objects 
$FormElements.Forms       | Clip
$FormElements.InputFields | Clip
$FormElements.ParsedHtml  | Clip
$FormElements.Content     | Clip
$FormElements.Scripts     | Clip
$FormElements.AllElements | Clip

# Get all page source elements page Text
$FormElements.AllElements | 
Select-Object -Expand OuterText 

$FormElements.AllElements | 
Select-Object -Expand InnerText

# Get all tagname elements
$FormElements.AllElements | 
Where{$PSItem.TagName -eq 'a'}

$FormElements.AllElements | 
Where{$PSItem.TagName -eq 'input'}

$FormElements.AllElements | 
Where{$PSItem.TagName -eq 'Button'}

$FormElements.AllElements | 
Where{$PSItem.TagName -eq 'Button'} | 
Select-Object outerHTML

$FormElements.AllElements | 
Where{$PSItem.TagName -eq 'Button'} | 
Select-Object -Expand OuterText

$FormElements.AllElements | 
Where{$PSItem.TagName -eq 'Button'} | 
Select-Object -Expand InnerText


# Get only the submit button that matches the criteria
($FormElements.AllElements | 
Where{$PSItem.TagName -eq 'Button'} | 
Select-Object -Property outerHTML) -Match 'SIGN IN'

$FormElements.AllElements | 
Where{$PSItem.TagName -eq 'Button'} | 
Select-Object -Property * | 
Where innerText -eq 'SIGN IN'



# Use Internet Explorer interactively
$url        = 'https://www.tutorialspoint.com/videotutorials'

$ie         = New-Object -com internetexplorer.application
$ie.Visible = $true
$ie.Navigate($url)

while ($ie.Busy -eq $true) 
{ Start-Sleep -Seconds 1 }

$ie.Document.anchors

# or 

$ie.Document.links

    

答案 1 :(得分:0)

似乎$search可能有多个结果并且是一个数组。 .Click()不适用于对象数组。

您可能还想使用以下方法将IE设置为“可见”以进行故障排除: $ie.Visible = $true

最好不要使用className,因为可以在同一页面中重用相同的类名。我怀疑这可能是导致多个$search结果的原因。最好在href上进行匹配,并同时使用Select-Object -First 1来限制结果。

相关问题