首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python-Selenium-如何找出它是窗口还是iframe或弹出

Python-Selenium-如何找出它是窗口还是iframe或弹出
EN

Stack Overflow用户
提问于 2014-02-20 20:28:24
回答 1查看 2.4K关注 0票数 0

我正在尝试使用自动化一些测试用例。我点击一个按钮打开一个新窗口/iframe/弹出。它需要在文本框中添加一些数据,然后从拖放框中选择一些数据。我有两个问题,

  1. 如果它是从Mozilla的火道上弹出的窗口/iframe/弹出,我将如何查找?
  2. 怎么弄到手柄?

这是当我选择新窗口/iframe/弹出(图像附件),html/body/div[11]时在火路径中看到的xpath和内容。

我这里有密码,zenoss_url = "http://xxxxx/" xpath_uname = "html/body/form/div/div/input[1]" xpath_pwd = "html/body/form/div/div/input[2]" xpath_submitButton = "html/body/form/div/div/input[3]" xpath_eventsTab = "html/body/div[7]/div/div/div/div[2]/ul/li[2]/div/div/a" driver = webdriver.Firefox() driver.get(zenoss_url) handle = driver.find_element_by_xpath(xpath_uname) handle.send_keys(zenoss_uname) handle = driver.find_element_by_xpath(xpath_pwd) handle.send_keys(zenoss_pwd) driver.find_element_by_xpath(xpath_submitButton).submit() driver.implicitly_wait(10) driver.find_element_by_xpath("html/body/div[7]/div/div/div/div[2]/ul/li[2]/div/div/a").click() driver.find_element_by_xpath("html/body/div[3]/div/div/div/div[1]/div/div/div[1]/div/div/div[7]/em/button").click() driver.implicitly_wait(5) window = driver.switch_to_window(?????)

我不知道我该用什么“名字”来代替?

感谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2014-02-20 21:02:00

html/body/div11 11的路径只是框架范围内的xpath。当导航到框架和子帧时,可以采取以下几种方法:从查找框架的索引到查找框架的名称。如果使用火路径检查框架内的元素,它只会为元素提供该框架的范围。框架就像彼此之间的盒子一样工作;为了进入第二个盒子,你必须打开第一个盒子,然后往里面看。

代码语言:javascript
运行
复制
//In C# 
driver.switchTo().Frame("FirstFrameFound"); 
//To close the box again and get back to the overall main box. 
driver.switchTo().defaultContent(); 

如果你的第二帧是第一帧的子帧,你必须跳入第一帧,然后切换到第二帧,与内部元素交互。

代码语言:javascript
运行
复制
//In C# 
//Switch to the first frame 
driver.switchTo().Frame("FirstFrameFound"); 
//Switch to the second frame 
driver.SwitchTo().Frame("SecondFrame"); 
//Now look for your element that you wish to interact with. 
driver.FindElement(By.XPath("//html/body/div[11]")); 
//To close the box again and get back to the overall main box. 
driver.switchTo().defaultContent(); 

请在这里查看这篇文章,以获得更多信息。

How to navigate a subframe inside a frameset using Selenium WebDriver?

如果您切换到一个窗口,也有多种方法。我喜欢的方法是这种方法:

代码语言:javascript
运行
复制
//When dealing with new window and the parent. 
//Switch to the last window that opened
driver.SwitchTo().Window(driver.WindowHandles.Last()); 
//Perform your actions
driver.Close(); 
//Switch back to the first window 
driver.SwitchTo().Window(driver.WindowHandles.First()); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21918821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档