Powershell anchor tag not found after first click in the same page

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Peter Jurgens Peter Jurgens 5 months, 2 weeks ago.

  • Author
    Posts
  • #59842
    Profile photo of Nagaraju Gajula
    Nagaraju Gajula
    Participant

    Hi,
    I am trying to automate the website login and clicking few links after that. Everything looks good until login and first click, but after that i could not find the link control and getting null-valued exception.

    In the Home page, i have 3 links. It works for the first link then after that it can't find the 2nd link and so on.

    Sample Code
    ————
    $username = "xyz"
    $password = "pwd"
    $url = "http://abc.com/"

    # Create an Internet Explorer object
    $ie = New-Object -ComObject 'internetExplorer.Application'
    $ie.Visible= $true # Make it visible

    # Open the website
    $ie.Navigate($url)

    do {sleep 1} until (-not ($ie.Busy))

    # Assign the DOM to the $doc variable
    $doc = $ie.document

    # Assign Username and Pwd
    $usernameField = $doc.getElementById('username')
    $usernamefield.value = 'username'

    Start-Sleep 1

    $passwordfield = $doc.getElementByID('Password')
    $passwordfield.value = 'password'

    # Wait for 2 seconds before click on Login button
    Start-Sleep 2

    # Click Login Button
    $submitButton = $doc.getElementByID("Login")
    $submitButton.click();

    Start-Sleep 2

    $Link1= $doc.getElementByID("Link1")
    $Link1.click();

    while ($ie.Busy -eq $true){Start-Sleep -seconds 4;}

    Start-Sleep 10
    $Link2= $doc.getElementByID("Link2")
    $Link2.click();

    while ($ie.Busy -eq $true){Start-Sleep -seconds 4;}

    Start-Sleep 5
    $Link3= $doc.getElementByID("Link3")
    $Link3.click();

    Please let me know where did i make a mistake.

    Thanks in Advance

    Nagaraju

  • #59845
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Powershell is not the best choice to automate keyboard and mouse actions. There are easier tools to do something like this. What do you want to do on the website? Powershell is able to interact with websites/webservers.

    • #59848
      Profile photo of Nagaraju Gajula
      Nagaraju Gajula
      Participant

      We would like to display the website in the office break room tv which would display live data automatically. Please let me know if you know any tools to do the same.

      Thanks

  • #59854
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Hmmm .... browsers are able to display websites. 😉 Why do you need a script for that?

  • #59859
    Profile photo of Peter Jurgens
    Peter Jurgens
    Participant

    Not 100% sure but I think you may need to set the $doc variable again before clicking link 2 since after clicking link 1 the DOM would reload.

    Try adding just another $doc=$ie.document right before your assignment of $link2 and the same for $link3.

You must be logged in to reply to this topic.