如何使用 firebug/Firepath 为 iframe 上的 webelement 编写 Xpath?

How to write Xpath for webelement which are on iframe using firebug/Firepath?

  1. 我可以使用或不使用 Firebug/ Firepath 来编写 Xpath,但如果可以使用这些第三方工具将 Xpath 与 iframe 一起编写,那就太好了。

  2. 首先我需要移动到 iframe,然后为 webElements 编写常规 Xpath

使用 xpath 查找 iframe。然后使用 getattribute() 获取 src。然后导航到 src.

例如:

var myFrame = driver.FindElement("descendant::iframe[@name='TheIFrame']");
string frameSrc = myFrame.GetAttribute("src");
driver.Navigate().GoToUrl(frameSrc);
//now you're on the frame and interact with its DOM

如果您不想离开当前所在的页面,您有以下几种选择:

1) 创建一个新的驱动程序并导航到那里

2) 使用 httpclient(比驱动程序的开销更少)通过向 src url 发送 get 来获取内容,然后使用 HTMLAgilityPack 将其加载到 HTMLDoc 中,您可以使用 XPath 导航 - 当然这将允许您抓取数据,但您将无法单击按钮或与 javascript.

进行交互