【问题标题】:Find all elements on a webpage using powershell/selenium only仅使用 powershell/selenium 查找网页上的所有元素
【发布时间】:2020-06-07 09:46:40
【问题描述】:

我需要在 Selenium 中使用 powershell。 我能够获得任何特定标记名的所有元素。 但是我如何引用特定元素。 比如说我想在主页上找到“开发者”按钮并点击它。

$browser = Start-SeChrome
$URL = "https://stackoverflow.com/"
$browser.Navigate().GoToURL($URL)
$A_Elements = Find-SeElement -Driver $browser -TagName a
$A_Element = $A_Elements|ForEach-Object{if($_.GetAttribute('Text') -eq 'For developers'){return $_}}
Invoke-SeClick -Driver $browser -Element $A_Element

我的脚本在第 5 行失败,因为它没有返回任何内容。 有任何想法吗。请帮忙。 谢谢

【问题讨论】:

    标签: powershell selenium


    【解决方案1】:

    听起来你正在使用Selenium PowerShell Module

    您应该尝试像这样指定元素 id:

    $Driver = Start-SeChrome 
    Enter-SeUrl https://stackoverflow.com/ -Driver $Driver
    $Element = Find-SeElement -Driver $Driver -Id "btn"
    Invoke-SeClick -Element $Element
    

    【讨论】:

      【解决方案2】:

      这终于奏效了。 它还为我提供了循环遍历元素的优势,无论它们是 input、span、div 等,并根据需要选择一个或多个元素。

      ForEach ($Input_Element in (Find-SeElement -Driver $browser -TagName input))
         {
         $Input_Element.GetAttribute('id')
         if ($Input_Element.GetAttribute('id') -eq 'login'){$Input_Element.SendKeys($FLogin) }
         if ($Input_Element.GetAttribute('id') -eq 'loginpw'){$Input_Element.SendKeys($Fpass) }
         if ($Input_Element.GetAttribute('id') -eq 'loginsubmit')
            {
            $Input_Element.Click() 
            Break
            }
         }

      【讨论】:

        猜你喜欢
        • 2013-11-10
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多