getElementsByName in IE7
In case you don't know why this isn't working in IE, here is the MSDN documentation on that function:
When you use the getElementsByName method, all elements in the document that have the specified NAME attribute or ID attribute value are returned.
Elements that support both the NAME attribute and the ID attribute are included in the collection returned by the getElementsByName method, but elements with a NAME expando are not included in the collection; therefore, this method cannot be used to retrieve custom tags by name.
Firefox allows getElementsByName()
to retrieve elements that use a NAME expando, which is why it works. Whether or not that is a Good Thing™ may be up for debate, but that is the reality of it.
So, one option is to use the getAttribute()
DOM method to ask for the NAME attribute and then test the value to see if it is what you want, and if so, add it to an array. This would require, however, that you iterate over all of the nodes in the page or at least within a subsection, which wouldn't be the most efficient. You could constrain that list beforehand by using something like getElementsByTagName()
perhaps.
Another way to do this, if you are in control of the HTML of the page, is to give all of the elements of interest an Id that varies only by number, e.g.:
<div id="Change0">...</div>
<div id="Change1">...</div>
<div id="Change2">...</div>
<div id="Change3">...</div>
And then have JavaScript like this:
// assumes consecutive numbering, starting at 0
function getElementsByModifiedId(baseIdentifier) {
var allWantedElements = [];
var idMod = 0;
while(document.getElementById(baseIdentifier + idMod)) { // will stop when it can't find any more
allWantedElements.push(document.getElementById(baseIdentifier + idMod++));
}
return allWantedElements;
}
// call it like so:
var changes = getElementsByModifiedId("Change");
That is a hack, of course, but it would do the job you need and not be too inefficient compare to some other hacks.
If you are using a JavaScript framework/toolkit of some kind, you options are much better, but I don't have time to get into those specifics unless you indicate you are using one. Personally, I don't know how people live without one, they save so much time, effort and frustration that you can't afford not to use one.
On IE document.getElementsByName won't work
Well, the problem is this: IE understands document.getElementsByName(...)[0] as document.getElementById(...). So if you would define also an id for your element, the method document.getElementsByName(element_name)[0].innerHTML="" will surprisingly also work in IE!
But since you anyway need to define an id due to IE, and since an id must always start with a char first, you must use:
<div id="a234">
<img src="pic.gif" height="70" width="100" onMouseOver="clear('a234')">
</div>
And this command:
function clear(element_id){
document.getElementById(element_id).innerHTML="";
}
Even more, document.getElementsByName(...)[0] is slower in Firefox: http://www.uize.com/tests/performance/getElementById-vs-getElementsByName.html
So the id definitely wins the race.
UPDATE:
Also important is the fact, that we can adress every id by #a234{...} in a CSS file. So we can define an own style for every id, and this makes the id even more powerful.
document.getElementsByName.length return 0 in IE
Your HTML is invalid. The span
element cannot have a name
attribute.
Internet Explorer appears to be ignoring the name
attribute on elements where it is forbidden. If you change a span
to an input
it will show up in the list.
If you want to describe a group of elements for referencing with JavaScript, use a class
with getElementByClassName
.
Using getElementsByname to fill in form fields with IE object but value not getting set?
Angular/SPA are a pain to access with COM. Sometimes you have to go at it "the other way around". First get all elements by Tag name (input) and then filter by name:
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("https://www.google.com")
while($ie.Busy) {
Start-Sleep -Milliseconds 100
}
$ie.visible=$true
$doc=$ie.Document
$inputTags = $doc.getElementsByTagName("input")
$SearchBox = ($inputTags |Where-Object {$_.name -eq 'q'})
$SearchBox.value = "Visual Studio"
Or, if you are specifically doing a Google search, call the URL directly:
https://www.google.com/search?q=Visual+Studio
Replacing spaces with +
plus signs:
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("https://www.google.com/search?q=Visual+Studio")
Does getElementById() and getElementByName() are same for IE9?
This is a 'feature' of IE. Their implementation of getElementById
initially searches for elements with the given id
attribute. If none are found, it then searches for elements by the name
attribute, which is against the spec.
In your example the behaviour of Firefox is correct, as your input
has no id
property.
If you want to find elements by their name, use the getElementsByName()
method instead.
call .document.getElementByName when no ie object is launched
Easy if you do the launch via selenium. Much harder if not impossible (haven't yet found a vba method) to attach to an existing browser instance e.g. one launched from command line that is different from that used by the current WebDriver instance.
I am gonna go out on a limb and say that because your commandline stuff is done via Shell
, and is only to launch Chrome, then you can simply replace with installing selenium basic, adding a reference to selenium type library
and then using selenium to open Chrome as follows:
Option Explicit
Public Sub OpenChromeAndSelect()
'References to Selenium type library
Dim d As WebDriver, el As WebElement
Set d = New ChromeDriver
Const URL = "https://clients.mindbodyonline.com/Report/Sales/Sales/Generate?reportID=undefined"
With d
.Start "Chrome"
.get URL
Set el = .FindElementById("myID")
'Do other stuff
'.Quit
End With
End Sub
Javascript: show hidden stuff in IE7
Apparently, IE7 doesn't support getElementsByName
.
Since I only had 3 things I needed to mess with, I used getElementById
...
However.. with getElementById
being used all the time would get nasty if there are a lot of things needing to be shown / hidden for whatever reason
InternetExplorer Com Object, getElementById, getElementsByName and getElementsByTagName
You should look into using WATIN from PowerShell, WATIN would make your life much easier.
Related Topics
How to Make API Call with Hooks in React
How to Search JSON Tree with Jquery
How to Put an Image File in a JSON Object
Multiple Path Names for a Same Component in React Router
How to Add Predefined Length to Audio Recorded from Mediarecorder in Chrome
What Does the Function Then() Mean in JavaScript
Angularjs Multiple Filter with Custom Filter Function
Array.Push() Makes All Elements the Same When Pushing an Object
How to Do Two-Way Filtering in Angularjs
How to Give Keyboard Focus to a Div and Attach Keyboard Event Handlers to It
Split String Only on First Instance of Specified Character
How to Rotate a 3D Object on Axis Three.Js
How to Trigger a Link's (Or Any Element'S) Click Event Through JavaScript
JavaScript Regular Expressions and Sub-Matches
How to Check If the Url Contains a Given String