2015-11-06 61 views
1

我想从下面的HTML中的下拉菜单中选择一个项目。PowerShell Internet Explorer Com对象选择类下拉菜单项

<select class="select" name="expiration"> 
 
<option value="N" selected="selected">Never</option> 
 
<option value="10M">10 Minutes</option> 
 
<option value="1H">1 Hour</option> 
 
<option value="1D">1 Day</option> 
 
<option value="1W">1 Week</option> 
 
<option value="2W">2 Weeks</option> 
 
<option value="1M">1 Month</option>

这里是我当前的代码。我没有收到任何错误,只是该项目没有被选中。

# Code to select menu item 
$ie = New-Object -comobject InternetExplorer.Application 
$ie.visible = $False 

# navigate to URL 
$ie.navigate('http://URL') 
while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 1000; } 

$expiration = $ie.Document.getElementsByClassName('expiration') 
$expiration.outerText |Select-Object -Index 2 
$ie.Document.getElementById('submit').Click() 
Start-Sleep -Milliseconds 1000 
$result = $ie.LocationURL 
+0

你尝试 - $ expiration.Options.SelectedIndex = 2? – Avshalom

+0

是的,无法在此对象上找到'SelectedIndex'属性。验证该属性是否存在并可以设置。 – rvrsh3ll

+0

'$ expiration = $ ie.Document.getElementsByClassName('expiration')'这行后面的'$ expiration'的值是多少?你的'select'元素的'class'是'select'而不是'expiration'。此外,我不认为你可以选择一个网页上的列表中的元素与PO的“选择”,它只会过滤你输入的内容,但不会在网页上执行 – sodawillow

回答

1

试试这个:

$ie = New-Object -ComObject InternetExplorer.Application 
$ie.Visible = $False 
$ie.navigate("http://URL") 
while ($ie.Busy) { Start-Sleep -Milliseconds 1000 } 

$expiration = $ie.Document.getElementsByClassName("select") 
$expiration.Options.SelectedIndex = 2 
$ie.Document.getElementById("submit").Click() 
Start-Sleep -Milliseconds 1000 
$result = $ie.LocationURL