Link to home
Create AccountLog in
HTML

HTML

--

Questions

--

Followers

Top Experts

Avatar of PavelTMN
PavelTMN

getElementsByTagName() not working in IE9
I want to automate some web browsing using Powershell and IE9 but the next problem arises:
$doc.getElementsByTagName("span")

Open in new window

returns
Cannot find an overload for "getElementsByTagName"  and the argument count: "1".
C:\Users\Admin\Desktop\lookmart.ps1:70 char:28
+   $doc.getElementsByTagName <<<< ("span")
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Open in new window

Everything's okay if I turn on the compatibility mode in IE9 but for some sites it cannot be enabled.

How do I change the code for IE9?

Thank you.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of BigRatBigRat🇫🇷

What EXACTLY is $doc?

The standard says that getElementsByTagName is a method of document or document fragment and not a node.

Avatar of PavelTMNPavelTMN

ASKER

What EXACTLY is $doc?

The full code. For illustrative purpose it navigates to slashdot.org, puts "java" string into a search text box and clicks to send the query. Throws exceptions at getElementById() and getElementsByTagName() when slashdot.org is excluded from the compatibility mode.

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("about:blank")
$doc = $ie.Document
$ie.visible = $true

Function NavigateTo([string] $url, [int] $delayTime = 2000)
{
  Write-Verbose "Navigating to $url"; 
  $ie.Navigate($url) 
  WaitForPage $delayTime
}

Function WaitForPage([int] $delayTime = 2000)
{
  $loaded = $false 
  while ($loaded -eq $false) {
    [System.Threading.Thread]::Sleep($delayTime)  
    #If the browser is not busy, the page is loaded
    if (-not $ie.Busy)
    {
      $loaded = $true
    }
  }
 
  $doc = $ie.Document
}

Function SetElementValueByID($id, $value) {
  if ($doc -eq $null) {
    Write-Error "Document is null";
    break
  }  
  
  $element = $doc.getElementById($id)
  if ($element -ne $null) {
    $element.Value = $value
  }
  else {
    Write-Warning "Couldn't find any element with id ""$id""";
  }
}

Function ClickElementByTagName($tagName, [int] $position = 0)
{
  if ($doc -eq $null) {
    Write-Error "Document is null"
    break
  }
  $elements = @($doc.getElementsByTagName($tagName))
  if ($elements.Count -ne 0) {
    $elements[$position].Click()
    WaitForPage
  }
  else {
    Write-Error "Couldn't find element ""$tagName"" at position ""$position""";
    break
  }
}

NavigateTo "http://slashdot.org/"
SetElementValueByID "searchquery" "java"
ClickElementByTagName "input" 1

Open in new window


Avatar of BigRatBigRat🇫🇷

Hmm. Seems OK, I'll have to have a think about that.

(The problems I have seen are all related to a node being used and not a document)

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Avatar of PavelTMNPavelTMN

ASKER

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

No viable solution was offered by experts
HTML

HTML

--

Questions

--

Followers

Top Experts

HTML (HyperText Markup Language) is the main markup language for creating web pages and other information to be displayed in a web browser, providing both the structure and content for what is sent from a web server through the use of tags. The current implementation of the HTML specification is HTML5.