Python:如何使用Selenium在IFrame之间切换, Selenium获取IFrame, webdriver, How to Switch Between IFrames Using Selenium Python

 

当我们希望在网页上托管来自外部来源的内容时,我们更喜欢使用IFrame。它可以是图像,视频,其他供应商的广告,突出显示某些信息等。

HTML提供了“ <iframe> </ iframe>”标签来标识HTML文档中的IFrame。

 

使用Selenium Python在Iframe之间切换

Python:如何使用Selenium在IFrame之间切换, Selenium获取IFrame, webdriver, How to Switch Between IFrames Using Selenium Python
Python:如何使用Selenium在IFrame之间切换, Selenium获取IFrame, webdriver, How to Switch Between IFrames Using Selenium Python

如果一个网页包含多个iframe,则需要在它们之间进行切换。Selenium Python API提供了“ switch_to.iframe (self, frame_reference) ”方法来移至特定的IFrame。

driver.switch_to.iframe(self,frame reference)

“<frame_reference>”参数是用于标识该IFrame的定位器。

 

让我们看一个示例HTML代码,它将在网页中创建多个IFrame。

<!DOCTYPE html>
<html>
<head>
<title>Switching Between IFrames Demo</title>
</head>
<body>
<h1>Welcome Viewers</h1>
<iframe name="frame1" id="FR1" src="//www.techbeamers.com" height="500" width="400"> </iframe>
<iframe name="frame2" id="FR2" height="500" width="400" src="http://www.seleniumhq.org"> </iframe>
</body>
</html>

此网页中嵌入了两个IFrame。要首先在上述IFrame之间进行切换,我们必须在网页上找到它们。看一下代码;它提供了三种不同的机制来执行此操作。他们是。

–通过使用标签名称(在本例中为“ iframe”)

–使用IFrame的ID

–使用IFrame的名称

 

这是在帧之间执行切换的指定代码段。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

driver = webdriver.Firefox()

driver.maximize_window()

location = "file://<Specify Path to IFrame.HTML>"

driver.get(location)


########Section-1

# get the list of iframes present on the web page using tag "iframe"
seq = driver.find_elements_by_tag_name('iframe')

print("No of frames present in the web page are: ", len(seq))

# switching between the iframes based on index
for index in range(len(seq)):

    driver.switch_to_default_content()
    iframe = driver.find_elements_by_tag_name('iframe')[index]
    driver.switch_to.frame(iframe)
    driver.implicitly_wait(30)

    # highlight the contents of the selected iframe
    driver.find_element_by_tag_name('a').send_keys(Keys.CONTROL, 'a')
    time.sleep(2)

    # undo the selection within the iframe
    driver.find_element_by_tag_name('p').click()
    driver.implicitly_wait(30)

driver.switch_to.default_content()


########Section-2

# switch to a specific iframe (First frame) using Id as locator
iframe = driver.find_element_by_id('FR1')
driver.switch_to.frame(iframe)
time.sleep(2)
driver.find_element_by_id('s').send_keys("Selected")
driver.switch_to.default_content()


########Section-3

# switch to a specific iframe (Second frame) using name as locator
iframe = driver.find_element_by_name('frame2')
driver.switch_to.frame(iframe)
time.sleep(2)
driver.find_element_by_tag_name('a').send_keys(Keys.CONTROL, 'a')

让我们逐步分析以上代码。

 

1)首先,必须将上面给出的HTML代码作为IFrame.HTML保存在计算机上。

 

2)接下来,您必须在上述代码段中提供的占位符中提供正确的路径。指定网页的文件路径时,必须使用正斜杠。否则,它可能无法正常工作。例如,在这里,我给的文件路径为。

location = "file://C:/Users/Automation-Dev/Desktop/selenium/IFrame.HTML"

 

3)在代码的第1节中,

seq= driver.find_elements_by_tag_name('iframe')

提供网页上存在的iframe列表。

 

4)我们使用以下步骤遍历此列表,从而在IFrame之间进行切换。

 iframe = driver.find_elements_by_tag_name('iframe')[index]
 driver.switch_to.frame(iframe)

 

5)每当您需要从IFrame移回父HTML时。Selenium Webdriver提供以下方法。

driver.switch_to.default_content()

 

6)在第2节中,我们使用定位器作为“ id”切换到特定的IFrame。

 iframe = driver.find_element_by_id('FR1')

 

7)在第3节中,我们使用定位器作为“名称”切换到特定的IFrame。

iframe = driver.find_element_by_name('frame2')

 

快速总结–在Iframe之间切换

了解如何使用Selenium Python在IFrame之间切换非常重要。您可以重新使用此技术来解决项目中的实时用例。

 

 

本文:Python:如何使用Selenium在IFrame之间切换, Selenium获取IFrame, webdriver, How to Switch Between IFrames Using Selenium Python

Loading

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.