gpt4 book ai didi

c# - 找到并切换到 iframe,但在其中找不到 IWebElement

转载 作者:太空宇宙 更新时间:2023-11-03 10:56:01 27 4
gpt4 key购买 nike

html代码:

<iframe title="javascript:''" src="PageName.aspx" _events="[object Object]">
<htlml>
<head>
<body>
<form name="FormName">
<div>
<span>
<input name="ButtonName>

我的问题:如何找到名称为“ButtonName”的元素?我当前的 C# 代码

//To find the iframe
IWebElement Object = driver.FindElement(By.XPath("XPath to the iframe"); //works
//To switch to and set focus to the iframe
driver.SwitchTo().Frame(Object); //works

//To find element with name "ButtonName"
IWebElement Button = driver.FindElement(By.Name("ButtonName")); //error: cannot find the element

感谢任何帮助。

最佳答案

请使用:驱动程序.FindElement(By.Name("ButtonName"));

试试这段代码:

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<iframe src="Default2.aspx"></iframe>
</body>
</html>

Default2.aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span>
<input type="button" name="ButtonName" class="test" onclick="alert(1);" />
</span>
</div>
</form>
</body>
</html>

我通过 visual studio 运行了 Default.aspx。然后我在我的控制台应用程序中使用我的 Default.aspx 的 url。我在控制台应用程序的主要功能中编写了以下代码:

FirefoxDriver driver = new FirefoxDriver
{
Url = "http://localhost:13764/WebSite1/Default.aspx"
};

IWebElement objecElement = driver.FindElement(By.XPath("//html//body//iframe"));
driver.SwitchTo().Frame(objecElement);
driver.FindElement(By.Name("ButtonName")).Click();

当我运行我的控制台应用程序时,我在我的 Firefox 窗口中看到一个警告,它是由上面的代码启动的。

关于c# - 找到并切换到 iframe,但在其中找不到 IWebElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19497249/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com