首页 > 解决方案 > 带有 Selenium 的 Powershell:选择下拉项

问题描述

我正在使用带有 Selenium 的 Powershell,需要从下拉列表中选择一个项目。该页面是https://app.beefy.finance/。我需要将“Vault Type”从“ALL”更改为“Single assets”

$browser = Start-SeChrome
$URL = "https://app.beefy.finance/"
$browser.Navigate().GoToURL($URL)
#----- Wait statements here ------
$Select_Option = $browser.FindElementById("select-vault-type")

#At this stage I have tried the following commands but they all error out:
Set-SeSelectValue -By Value -value 'Single Assets' -Element $Select_Option
$Select_Option.SelectByValue('Single Assets')
$Select_Option.Text('Single Assets')

我什至尝试过使用 xpath,但也失败了

标签: powershellselenium

解决方案


您需要单击下拉列表才能显示对象。然后你可以找到它并点击它。我使用它的 XPath:

$browser = Start-SeChrome
$URL = "https://app.beefy.finance/"
$browser.Navigate().GoToURL($URL)
#----- Wait statements here ------
$Select_Option = $browser.FindElementById("select-vault-type")
$Select_Option.Click()
$Select_Option.FindElementByXPath('/html/body/div[4]/div[3]/ul/li[2]').Click()

推荐阅读