Location>code7788 >text

[python][selenium] Web UI automation page switching iframe frame

Popularity:588 ℃/2024-09-05 23:19:48

Related Article:8 Ways to Position Page Elements for Web UI Automation




1, switch iframe method: switch_to.frame
  There are 4 types of entry into the Senate:
  1.1、id
  1.2、name
  1.3 Index
  1.4. iframe element object

2, return to the main document (the outermost page) method: switch_to.default_content ()
3、Return to the previous level iframe method: switch_to.parent_frame()

 


Briefly:
The iframe element is used to nest an html within another element in the current html.
Because webdriver can only recognize and manipulate elements on a page by default, it cannot directly locate and manipulate the main document html and inline html.
To locate the element that manipulates the inline page of an iframe, you need to switch to the iframe first.

Below I use Netease cloud music home page to do demo, directly with the code + comments to demonstrate the instructions.

 


1、Switching the iframe method of the 4 kinds of input parameters

1.1、id

from selenium import webdriver
from import By
from time import sleep


driver = ()
driver.maximize_window()
driver.implicitly_wait(10)
("https://music./")

# manipulate:Switching from the page master document to the (used form a nominal expression)iframe,Positioning the right arrow button on the NetEase Cloud Music rotator image,expense or outlayforCycle Click30substandard
driver.switch_to.frame("g_iframe")
ele = driver.find_element(By.CLASS_NAME, "btnr")
for i in range(30):
    ()
    sleep(0.1)

sleep(3)
()

1.2、name

from selenium import webdriver
from import By
from time import sleep


driver = ()
driver.maximize_window()
# driver.implicitly_wait(10) # with implicitly_wait it takes about 10 seconds for the page to be positioned, without it it's very fast, the reason for this is still being researched
("https://music./")

# Operation: switch from the main document of the page to the iframe with name="contentFrame" and click the "User Login" button.
driver.switch_to.frame("contentFrame")
driver.find_element(, "index-enter-default").click()

sleep(3)
()

1.3 Index

from selenium import webdriver
from import By
from time import sleep


driver = ()
driver.maximize_window()
driver.implicitly_wait(10)
("https://music./")

# manipulate:Switch from the main document of the page to the first1classifier for individual things or people, general, catch-all classifieriframe,Click on the popular recommendations of the"more"buttons
driver.switch_to.frame(0)
driver.find_element(By.CSS_SELECTOR, 'span > a[href="/discover/playlist/"]').click()

sleep(3)
()

1.4. iframe element object

from selenium import webdriver
from import By
from time import sleep


driver = ()
driver.maximize_window()
driver.implicitly_wait(10)
("https://music./")

# manipulate:localize toiframeelemental,把elemental传给switch_to.frameMethod switchingiframe,Click on the bottom of the page"Music Open Platform"
iframe = driver.find_element(By.CSS_SELECTOR, "body > iframe")
driver.switch_to.frame(iframe)
driver.find_element(By.CSS_SELECTOR, 'a[href="./st/developer"]').click()

sleep(3)
()

2、Return to the main document (the outermost page)

If there is another iframe inside the iframe, you don't want to operate on the previous layer of iframes, and you want to go back to the main document directly, you can use this method.

from selenium import webdriver
from import By
from time import sleep


driver = ()
driver.maximize_window()
driver.implicitly_wait(10)
("https://music./")

# Switch the iframe first
driver.switch_to.frame("g_iframe")
# switch back to the main document (comment out this sentence, locating the main document element will report an exception: NoSuchElementException)
driver.switch_to.default_content()
# Locate the search box of the main document and enter the content.
driver.find_element(, "srch").send_keys("empty track")

sleep(3)
()

3、Return to the previous iframe

If there is another iframe inside the iframe, you don't want to go back to the main document, and you want to manipulate the previous iframe, you can use this method.

from selenium import webdriver
from import By
from time import sleep


driver = ()
driver.maximize_window()
driver.implicitly_wait(10)
("https://music./")

# switch iframe first (I haven't found any page with so many layers of iframes. I'll leave it as it is, the principle is the same)
driver.switch_to.frame("g_iframe")
# switch to the previous iframe (comment out this sentence, locating the elements of the previous iframe will report an exception: NoSuchElementException)
driver.switch_to.parent_frame()
# Locate the main document's search box and enter content
driver.find_element(, "srch").send_keys("empty_track")

sleep(3)
()