Python Selenium error and iframe tag content is missing from the source loaded by the web driver.

Asked 1 years ago, Updated 1 years ago, 104 views

There is an iframe tag on the website I am testing and I need to click the button (a tag) that exists in this tag. If you look at it with the developer tool in your browser, you can see that it contains content within the iframe tag, but if you look at the HTML loaded with page_source, there is nothing between the iframe tags.

<iframeid="conFrame" src="..."> # When viewed from the browser developer tool
    <html>
    ...
    </html>
</iframe>

<iframeid="conFrame" src="..."></iframe> #page_source

Because of this, it seems that if you just access the a tag in the iframe, the element cannot be found error occurs.

print(driver.find_element_by_tag_name("a"))

selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with tag name == a

An error occurs even if you designate a frame in the following way, but I wonder why this phenomenon occurs. Maybe it's because the load isn't complete, but even if I try to approach it with enough term with sleep() function, the error continues to occur.

# Specify Frame
driver.switch_to.frame(driver.find_element_by_id("conFrame"))
driver.switch_to.frame("conFrame")

I wonder why this phenomenon occurs and how to access the a tag within the iframe. I ask for your help me.

InternetExplorer 11.953.14393.0 , IEDriverServer.exe

selenium python iframe webdriver html

2022-09-22 15:27

1 Answers

I think we should say switch_to_frame. When I made an html file with iframe locally and did it as below, I read the html in iframe well.

from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome('your/path/to/chromedriver')
driver.get('file:///your/path/a.html')

driver.switch_to_frame(driver.find_element_by_tag_name("iframe"));

print(driver.page_source)


2022-09-22 15:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.