当前位置: 首页 > 编程笔记 >

如何使用python处理Selenium中的帧?

江雅懿
2023-03-14
本文向大家介绍如何使用python处理Selenium中的帧?,包括了如何使用python处理Selenium中的帧?的使用技巧和注意事项,需要的朋友参考一下

我们可以处理Selenium中的帧。框架是一个HTML元素,用于将文档保留在页面中的另一个文档中。HTML具有<frame>或<iframe>标记,用于将框架嵌入文档中。

Selenium中有多个API可用于框架。它们在下面列出-

  • switch_to_frame(id)

    此方法用于在帧ID的帮助下识别帧,然后将焦点切换到该特定帧。

语法-

driver.switch_to_frame("frameid"), where frameid is the id attribute present under the frame/iframe tag in HTML.
  • switch_to_frame(名称)

    此方法用于在帧名称的帮助下识别帧,然后将焦点切换到该特定帧。

语法-

driver.switch_to_frame("framename"), where framename is the name attribute present under the frame/iframe tag in HTML.
  • switch_to_frame(webelement)

    方法用于借助框架Web元素识别框架,然后将焦点切换到该特定框架。

语法-

driver.switch_to_frame("frameclassname"), where frameclassname is the name of class attribute present under the frame/iframe tag in HTML.
  • switch_to_parent_frame()

    该方法用于出现在当前框架之外,然后我们可以访问该框架之外而不是该框架内部的元素。

  • switch_to_default_content()

    此方法用于从所有框架中移出并在页面上切换焦点。一旦我们搬出,它将无法访问页面中框架内的元素。

示例

带框架的代码实现。

from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://the-internet.herokuapp.com")
#to refresh the browser
driver.refresh()
driver.find_element_by_link_text("Frames").click()
driver.find_element_by_link_text("Nested Frames").click()
# to switch to frame with frame name
driver.switch_to.frame("frame-bottom")
# to get the text inside the frame and print on console
print(driver.find_element_by_xpath ("//*[text()='BOTTOM']").text)
# to move out the current frame to the page level
driver.switch_to.default_content()
#to close the browser
driver.quit()
 类似资料:
  • 本文向大家介绍如何使用python处理Selenium中的子窗口?,包括了如何使用python处理Selenium中的子窗口?的使用技巧和注意事项,需要的朋友参考一下 我们可以处理Selenium中的子窗口或选项卡。在使用子窗口时,我们需要始终将浏览器焦点转移到子窗口上,然后对其进行操作。 默认情况下,焦点保持在第一个父窗口上。Selenium中有多种可用的方法,以下列出- current_win

  • 问题内容: 我使用 Selenium来 启动浏览器。如何处理要求浏览器接受证书的网页(URL)? 我重复我的问题: 当我启动 使用Selenium(Python编程语言)的浏览器(Internet Explorer,Firefox和Google Chrome)时,如何自动接受网站的证书? 问题答案: 对于Firefox,您需要将 选项设置为 : 对于Chrome,您需要添加参数 argument:

  • 问题内容: 在这种情况下有一个under 。而且我必须选择外表去内在,并写在内在的身体上。 接下来,我必须从内部到外部出来,然后单击“确定”按钮(位于外部)。 以下是我的代码 以下是我的问题: 我的测试代码可以正常工作到第4行,即写到正文中,但是我想从内到外显示它未找到该元素。 我尝试使用索引,父母,亲戚,但没有运气。 注意: 如果我不选择内部框架()。我可以单击“确定”按钮。 问题答案: 在We

  • 问题内容: 我需要从Sellercentral.amazon.de下载大量的excel文件(估计:500-1000)。手动下载不是一种选择,因为每次下载都需要单击几下,直到Excel弹出。 由于亚马逊无法为我提供具有其结构的简单xml,因此我决定自行进行自动化。首先想到的是Selenium和Firefox。 问题: 必须登录到Sellercentral,以及2要素验证(2FA)。因此,如果我登录一

  • 代码错误 行,我在控制台中得到如下错误:无法找到元素:{“method”:“id”,“selector”:“loginusername”}命令持续时间或超时:262毫秒

  • 问题内容: 这是我的代码: 现在我有下一个错误: 线程“主”中的异常org.openqa.selenium.NoSuchElementException:无法找到ID == txtEnterCptCode的元素(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:404毫秒。 有任何想法吗? 问题答案: 看来您实际上并没有切换到任何新窗口。您应该获得原始窗口的窗口句柄,将其保存,然后获取新窗口