hdsouza
hdsouza

Reputation: 355

Find all elements on a webpage using powershell/selenium only

I need to use powershell with Selenium. I am able to get all the elements of any specific Tagname. But How do i reference a specific element. Let say for example I want to find the "For developers" button on the homepage and click it.

$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

My script fails on the 5th line, as it does not return anything. Any ideas. Please help. Thanks

Upvotes: 0

Views: 2150

Answers (2)

hdsouza
hdsouza

Reputation: 355

This finally worked. It also gives me the advantage of cycling through the elements, whether they are input, span, div etc, and selecting one or more elements as needed.

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
      }
   }

Upvotes: 0

mkk
mkk

Reputation: 128

Sounds like you are using Selenium PowerShell Module.

You should try specifying the element id like this:

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

Upvotes: 1

Related Questions