[PYTHON] I can't manipulate iframes in a page with Selenium

problem

I tried to get the contents of an iframe in a page using Selenium / BeautifulSoup, but I can't.

If you get all the HTML with BeautifulSoup, the result will be as follows.

Code to get HTML


BeautifulSoup(driver.page_source, 'html.parser')

Acquisition result


<html>
    <head>
        <!-- head -->
    </head>
    <body>
        <!-- body -->
        <iframe id="iframe" 
                scrolling="yes"
                src="https://example.com/iframe"
                src_data="https://example.com/iframe">
        </iframe>
    </body>
</html>

Thing you want to do

I want to see and operate in the iframe.

Solution

In the code below, the driver focuses on the iframe.

iframe = driver.find_element_by_id('#iframe')
driver.switch_to.frame(iframe)

Code to get HTML


BeautifulSoup(driver.page_source, 'html.parser')

Make sure the HTML is an iframe in.

reference

https://www.selenium.dev/documentation/ja/webdriver/browser_manipulation/#webelementを使う

Postscript

To return to the original window, use the following code.

driver.switch_to.default_content()

Recommended Posts

I can't manipulate iframes in a page with Selenium
I can't log in to the admin page with Django3
I can't get the element in Selenium!
When I get a chromedriver error in Selenium
Create a new page in confluence with Python
I was addicted to scraping with Selenium (+ Python) in 2020
I want to work with a robot in python.
I tried a simple RPA for login with selenium
I can't exe a project using PyWebView with PyInstaller
Scraping with Selenium in Python
I can't search with # google-map. ..
I-town page scraping with selenium
I made a simple typing game with tkinter in Python
Until I return something with a line bot in Django!
I can't send emails from Lambda in a VPC with Boto3 using a VPC endpoint for SES
I can't install python3 with pyenv-vertualenv
Scraping with Selenium in Python (Basic)
I get a KeyError in pyclustering.xmeans
I can't enter standard in Subprocess ...
I can't install mysql-connector-python with anaconda
I made a fortune with Python.
I want to create a graph with wavy lines omitted in the middle with matplotlib (I want to manipulate the impression)
I tried to make a periodical process with Selenium and Python
I can't install scikit-learn in Python
I wrote a script to extract a web page link in Python
I can't use Japanese with pyperclip
I made a daemon with Python
I tried to log in to twitter automatically with selenium (RPA, scraping)
I can't import modules in the parent directory even with sys.path.append ('..')
Get a capture of the entire web page in Selenium Python VBA
I had a hard time with ImportError: DLL load failed in tensorflow 2.0
I made a tool to automatically browse multiple sites with Selenium (Python)
(Matplotlib) I want to draw a graph with a size specified in pixels
I got stuck in a flask application redirect with a reverse proxy in between
I made a mistake in fetching the hierarchy with MultiIndex of pandas
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
I want to print in a comprehension
I made a payroll program in Python!
I made a character counter with Python
Draw a heart in Ruby with PyCall
I drew a heatmap with seaborn [Python]
Page cache in Python + Flask with Flask-Caching
I tried a functional language with Python
What I did with a Python array
I can't deploy with google app engine
I made a Hex map with Python
I made a life game with Numpy
Build a capture acquisition machine with Selenium
I started Node.js in a virtual environment
I created a password tool in Python.
I made a roguelike game with Python
I really wanted to copy with selenium
I made a simple blackjack with Python
I can't install the package with pip.
I made a configuration file with Python
I can't see new files with yum
I made a WEB application with Django
I made a neuron simulator with Python
Why can't I install matplotlib in python! !!
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.