[英]How to locate and click on an element which is nested within multiple frame and frameset through Selenium using Webdriver and C#
[英]How to recursively locate element within nested iframes in Selenium?
我找到了這兩個主題:
但它們不是基於 selenium 或 C#,您可能會在下面看到我到目前為止的內容:
public static IWebElement FindWebElementByXPath(ChromeDriver chromeDriver, string xPath, string frameType = "iframe")
{
Console.WriteLine(" Searching for Element: " + xPath + "\n");
Console.WriteLine();
IWebElement element;
try
{
chromeDriver.SwitchTo().DefaultContent();
element = chromeDriver.FindElement(By.XPath(xPath));
if (element != null)
{
Console.WriteLine(" Found Element: " + xPath + " (" + element.Text + ")\n");
Console.WriteLine();
return element;
}
}
catch (Exception)
{
// ignored
}
var windowHandles = chromeDriver.WindowHandles;
foreach (var window in windowHandles)
{
chromeDriver.SwitchTo().Window(window);
var frames = chromeDriver.FindElements(By.TagName(frameType));
foreach (var frame in frames)
{
chromeDriver.SwitchTo().DefaultContent();
chromeDriver.SwitchTo().Frame(frame);
try
{
element = chromeDriver.FindElement(By.XPath(xPath));
if (element != null)
{
Console.WriteLine(" Found Element: " + xPath + " (" + element.Text + ")\n");
Console.WriteLine();
return element;
}
}
catch (Exception)
{
// ignored
}
var frameChildren = chromeDriver.FindElements(By.TagName(frameType));
foreach (var child in frameChildren)
{
chromeDriver.SwitchTo().DefaultContent();
chromeDriver.SwitchTo().Frame(frame);
chromeDriver.SwitchTo().Frame(child);
try
{
element = chromeDriver.FindElement(By.XPath(xPath));
if (element != null)
{
Console.WriteLine(" Found Element: " + xPath + " (" + element.Text + ")\n");
Console.WriteLine();
return element;
}
}
catch (Exception)
{
// ignored
}
}
}
}
Console.WriteLine("Failed to find Element: " + xPath + "\n");
Console.WriteLine();
return null;
}
我當前的代碼只能處理 1 幀和所述幀的孩子,但不能處理孩子的孩子等等。
我的大腦很痛,因為我不知道如何編寫遞歸 function 來爬過所有這些。
這是我想出的解決方案:
public static IWebElement FindWebElementByXPath(ChromeDriver chromeDriver, string XPath, string FrameType = "iframe")
{
IWebElement element = null;
Console.WriteLine(" Searching for Element: " + XPath + "\n");
chromeDriver.SwitchTo().DefaultContent();
try
{
element = chromeDriver.FindElement(By.XPath(XPath));
if (element != null)
{
Console.WriteLine(" Found Element: " + XPath + " (" + element.Text + ")\n");
return element;
}
}
catch
{
}
var iframes = chromeDriver.FindElements(By.TagName(FrameType)).ToList<IWebElement>();
var index = 0;
Reset:
for (; index < iframes.Count; index++)
{
retry:
try
{
var item = iframes[index];
try
{
chromeDriver.SwitchTo().Frame(item);
try
{
element = chromeDriver.FindElement(By.XPath(XPath));
if (element != null)
{
Console.WriteLine(" Found Element: " + XPath + " (" + element.Text + ")\n");
return element;
}
}
catch
{
}
}
catch
{
chromeDriver.SwitchTo().ParentFrame();
goto retry;
}
var children = chromeDriver.FindElements(By.TagName(FrameType)).ToList();
if (children.Count > 0)
{
if (iframes.Any(x => children.Any(y => y.Equals(x))) == false)
{
iframes.InsertRange(index+1,children);
goto Reset;
}
}
}
catch
{
}
}
return element;
}
在這里,您是 go,從字面上看,SO 上唯一一個帶有用於在 C# Selenium 中爬行嵌套 iframe 的片段的命中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.