首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在使用powershell选择单选按钮时遇到困难

在使用powershell选择单选按钮时遇到困难
EN

Stack Overflow用户
提问于 2015-09-03 15:18:00
回答 2查看 2.4K关注 0票数 0

我试图让powershell脚本打开互联网浏览器,这样做,并去一个网站,并点击一个单选按钮。我收到的错误告诉我,与getElementsByTagName的行。它还说,我不能对下面所有行的空值表达式调用一个方法,并包括getElementsByTagName。

代码语言:javascript
运行
复制
$ie = New-Object -com "InternetExplorer.Application"
$ie.Navigate("hidden")
$ie.visible = $true



$doc = $ie.documentElement
$myradio = $doc.getElementsByTagName('radio')|?{$_.type -eq 'radio' -and $_.name -eq 'export'}

$x = 2 #specific radio button
$myradio.setActive()
$myradio.click()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-06 20:50:56

我想明白了,所以对于我来说,我必须首先启动$ie部分,然后用我的方式完成它。如果IE不打开Powershell将不会选择COM对象。一旦那部分被弄明白了,一切都像魅力一样运作。

代码语言:javascript
运行
复制
#gathers COm Objects for IE
$ie = New-Object -ComObject internetexplorer.application;
#Opens IE and navigates to the site
$ie.visible = $true;

foreach($s in $site){
$sName = $s.ServerName
$sLink = $s.Link
"`n"

$url = $site+$results

$ie.navigate($s.Link);

#sleeps for a second
    while ($ie.Busy -eq $true) 
    { 
        Start-Sleep -Milliseconds 1000; 
    } 

#gets export button radio and clicks it
$ie.document.getElementById('exp').click();

#sleeps for another second for page to load
    while ($ie.Busy -eq $true) 
    { 
        Start-Sleep -Milliseconds 1000; 
    } 

#Clicks search button for results page
$button = $ie.Document.getElementsByName('search') | Where-Object {$_.type -eq "button"}
$button.click();
票数 0
EN

Stack Overflow用户

发布于 2015-09-09 15:58:55

因此,使用您提供的URL作为测试,这似乎适用于为喜爱的颜色选择第一个单选按钮:

代码语言:javascript
运行
复制
$ie = New-Object -com "InternetExplorer.Application"
$ie.Navigate("http://webforms2.googlecode.com/svn/trunk/testsuite/021.html")
$ie.visible = $true

$doc = $ie.Document
$radioButtons = $doc.getElementsByTagName('input') | Where-Object {$_.type -eq 'radio' -and $_.name -eq 'favColor1'}

$x = 0 #specific radio button
$radioButtons[$x].setActive()
$radioButtons[$x].click()

$ie.documentElement似乎不存在,因此您为什么得到空值异常,而是需要$ie.document

接下来,您需要一个单选按钮数组来选择,在本例中需要使用的标记名称是输入,然后需要使用$x变量对单选按钮数组进行索引,以选择单击哪个按钮。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32379589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档