Powershell ..从网页中选择下拉菜单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Powershell ..从网页中选择下拉菜单相关的知识,希望对你有一定的参考价值。

我试图在网站上选择一个下拉菜单,但我完全失去了如何这样做。我用谷歌搜索了它,但没有找到任何运气。这就是我想要做的..

连接到网络..从下拉菜单设置数量中选择一个项目然后添加到购物车.............................. ..........................

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Web Link"
$objForm.Size = New-Object System.Drawing.Size(300,150) 
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
   {$tag=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
   {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(100,75)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$WebSite=$objTextBox.Text;$objForm.Close()})
$OKButton.TabIndex=1

$objForm.Controls.Add($OKButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Enter Website Page:"

$objForm.Controls.Add($objLabel) 

$objTextBox = New-Object System.Windows.Forms.TextBox 
$objTextBox.Location = New-Object System.Drawing.Size(10,40) 
$objTextBox.Size = New-Object System.Drawing.Size(260,20) 
$objTextBox.TabIndex = 0

$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})

[void] $objForm.ShowDialog()

#Connect to Website
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("$WebSite")
$ie.visible = $true
   while ($ie.Busy -eq $true) {Start-Sleep -Milliseconds 1000; }


i see this on the website, but i dont know hot to set the value or select the item. or is there easier way to
do this when the webpage loads?


#<select name="skuAndSize" class="size-dropdown mediumSelect footwear selectBox" style="display: none;">
         <option value=""></option>
            <option name="skuId" value="2870325:7"> 7</option>
            <option class="size-not-in-stock" name="skuId" value="2870326:7.5"> 7.5</option>
            <option name="skuId" value="2870327:8"> 8</option>
            <option class="size-not-in-stock" name="skuId" value="2870328:8.5"> 8.5</option>
            <option name="skuId" value="2870329:9"> 9</option>
            <option name="skuId" value="2870330:9.5"> 9.5</option>
            <option name="skuId" value="2870331:10"> 10</option>
            <option class="size-not-in-stock" name="skuId" value="2870332:10.5"> 10.5</option>
            <option name="skuId" value="2870333:11"> 11</option>
            <option class="size-not-in-stock" name="skuId" value="2870334:11.5"> 11.5</option>
            <option name="skuId" value="2870335:12"> 12</option>
            <option class="size-not-in-stock" name="skuId" value="2870336:12.5"> 12.5</option>
            <option name="skuId" value="2870337:13"> 13</option>
            <option class="size-not-in-stock" name="skuId" value="2870338:13.5"> 13.5</option>
            <option name="skuId" value="2870339:14"> 14</option>
            <option class="size-not-in-stock" name="skuId" value="2870340:15"> 15</option>
            <option class="size-not-in-stock" name="skuId" value="2870341:16"> 16</option>
            <option class="size-not-in-stock" name="skuId" value="2870342:17"> 17</option>
            <option class="size-not-in-stock" name="skuId" value="2870343:18"> 18</option>
       </select> 

#<span class="selectBox-label" data-qa="pdp.buyingtools.size-dropdown">(9.5)</span>#
答案

试试这个:

# will select item with label 9
$dropdown = $ie.Document.getElementById("skuAndSize")
($dropdown | where {$_.innerhtml -eq "9"}).Selected = $true
另一答案

找到了一个更好的解决方案,它将使用精确的文本选择值

定义一个函数

applicationList是select标记的id

test abc是要匹配的确切文本


function Subscription()
{
$x = $IE.document.getElementById("applicationList");
for($i=0;$i -lt $x.length;$i++)
{
If($x.options[$i].text -eq "test abc")
{
return $i;
}}}

调用函数

$index=Subscription;
$IE.Document.getElementById("applicationList").SelectedIndex=$index;

以上是关于Powershell ..从网页中选择下拉菜单的主要内容,如果未能解决你的问题,请参考以下文章

从网页中复制的下拉框粘贴到EXCEL中,如何导出下拉框中的数据,或者网页中下拉框的数据要怎样快速地采集

使用 php/ajax 选择下拉菜单后从数据库中获取客户信息

无法从下拉菜单中选择选项

网页下拉菜单怎么设置

使用 PHP 和 mySql 从下拉菜单中选择选项

用js或者jq实现,根据下拉菜单的字段来显示对应的内容?