I can not access webelement inside iframe

thusarix

New Member
Joined
Jun 14, 2017
Messages
9
If i try to navigate to the url of the iframe it redirects me to the main web page. With my code if i try to access the webelements inside the iframe i got no results

Code:
[COLOR=#333333]Sub wanabet()[/COLOR]<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">

Set IE = New InternetExplorerMedium
Dim HTMLDoc As HTMLDocument
Dim frames As IHTMLElementCollection
Dim fCounter As Long

      
    With IE
            
            .navigate "https://www.wanabet.es/index.html#!/sport/?params=home"
            .Visible = True
            Application.Wait (Now + TimeValue("0:00:10"))
            
    End With

 
Set frames = IE.document.getElementsByTagName("iframe")

For fCounter = 0 To frames.Length - 1
    
    If Left(frames(fCounter).ID, 9) = "iframe-sb" Then
        
        x = fCounter
        Exit For
    End If
Next

'this list has 0 items in it, which is wrong'
Set sporturi = frames(x).document.getElementsByClassName("KambiBC-navigation-menu__list-item")
x = sporturi.Length

 For Each sport In sporturi
    fotbal = sport.getElementByClassName("KambiBC-navigation-menu__label--title").innerText
    if fotbal = "Futbol"

      '''' do stuff'''

next sport 
End Sub</code>[COLOR=#333333]           fotbal = sport.getElementByClassName("KambiBC-navigation-menu__label--title").innerText[/COLOR]

Also i need to maximize the IE window as id does not show all details if not maximized, i do this manually.

if i try this line for getting the content of the iframe , i get "Access is denied" message:

Code:
Set sporturi = frames(x).contentWindow.document.getElementsByClassName("KambiBC-navigation-menu__list-item")
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Rather than asking us to debug your code, it is simpler if you specify what you wish to extract from the site and how this is achieved via the browser.

Bye
 
Upvote 0
Rather than asking us to debug your code, it is simpler if you specify what you wish to extract from the site and how this is achieved via the browser.

Bye
I want access the page that contains futbal events of current day
to extract the odds of each event, to build something like a odds comparator, i have done it on other betting sites but this iframe is really putting the breaks on my project.
 
Upvote 0
I am not familiar with tha site, that is why I also asked "how this is achieved via the browser"; ie what to click on the visible browse
 
Upvote 0
ie what to click on the visible browse

If by "visible browser" you mean the main web page and not the iframe, then the answer is nothing. The button needed to be clicked to get to the page that i need 'Fútbol' is inside the iframe.
 
Upvote 0
I have been working on this request; indeed access to the selected iframe (via the contentDocument property) is locked, even though the iframe source (https://bo.wanabet.es/uploads/2017/6/d2otfpiframe.html#/home) seems belonging to the same domain of the main page (https://www.wanabet.es/index.html#!/sport/?params=home).

It is definitively blocked for iframes that refers to different domains to protect against the Cross-site scripting security vulnerability

The contentDocument property returns the Document object generated by a frame or iframe element.

This property can be used in the host window to access the Document object that belongs to a frame or iframe element.

Note: Because of security reasons, the contents of a document can be accessed from another document only if the two documents are located in the same domain.

As I said, the main document and the iframe documents seem to belong to the same domain; so what happens is not fully clear to me, and I shall keep examining the case (subject tio time availability, of course).

Bye for now,
 
Last edited:
Upvote 0
It is definitively blocked for iframes that refers to different domains to protect against the Cross-site scripting security vulnerability

Well the fact is the in python i can
Code:
 [COLOR=#000000][FONT='inherit']switch_to.frame([/FONT][/COLOR][COLOR=#008080][FONT='inherit'][B]"iframe-sb"[/B][/FONT][/COLOR][FONT='inherit'][B][COLOR=#000000])[/COLOR][/B][/FONT]
and then access the elements inside the frame
 
Upvote 0
I am glad you found a solution via python.

I run additional tests that confirm that accessing the iframes information is possible; but accessing the html content of the iframe is possible if the iframe source url belongs to the same domain of the main page, but is forbidden if the iframe source url belongs to a different domain.

This has been tested by creating in a customer's web site a multiframes page with url "http://www.webdomain.com/TestPath/test.html"; it included 7 iframes: 4 of these, embed pages from the same web domain (in the form of www.webdomain.com/Path1/testpage1.html and www.webdomain.com/Path2/testpage2.html); the remaing 3 embed pages form different domains (www.domain2.org/page.html)

I got the iframes collection by using:
Code:
Set frameItems = ie.document.getElementsByTagName("iframe")

Then I was able to access (for example) the TD collection of the same domain frames, by using (for example)
Code:
Set lColl0 = frameItems(0).contentDocument.getElementsByTagName("TR")

Whereas it was forbidden accessing the inner collections of frameItems 4-5-6

This is however in contrast with what we observe on your main page, as the iframe with Id= "iframe-sb" belongs to the same domain of the main page.
I didn't found any information that suggests https sites should behave the way we observed.

I have no other option to test, so long live your python solution.

Bye
 
Upvote 0
Have you tried getting the src attribute from the iframe element and navigating to that URL? Then you may be able to access the document content within the iframe.
 
Upvote 0

Forum statistics

Threads
1,221,699
Messages
6,161,367
Members
451,700
Latest member
Eccymarge

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top