我正在尝试自动执行iframe中的点击。我删除了所有不必要的代码,并共享了导致问题的最少代码。这是提取的代码,
a.html->在iframe中指向b.html
<html>
<head></head>
<body>
<IFRAME width="100%" height="100%" id="iframe" src="b.html" style="HEIGHT: 100% !important; WIDTH: 100% !important">
</IFRAME>
</body>
</html>
b.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style>
DIV {
vertical-align: top;
font-family: Verdana;
font-size: 10px;
}
#left_container_menu {
position: absolute;
overflow-x: hidden;
overflow-y: scroll;
left: 9px;
width: 190px;
top: 30px;
bottom: -3px;
}
BODY {
vertical-align: top;
font-family: Verdana;
font-size: 10px;
}
BODY {
background-color: #ffffff;
}
BODY {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px;
overflow: auto;
}
HTML {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px;
overflow: auto;
}
.menu_content_in {
border-left: solid #D1D1D1 1px;
border-right: solid #D1D1D1 1px;
border-bottom: solid #D1D1D1 1px;
padding: 2px 0px 4px 2px;
}
A {
text-decoration: none;
color: black;
line-height: 14px;
margin: 3px;
}
</style>
</head>
<BODY>
<DIV id=left_container_menu>
<DIV id=ctl00_Menu_ctl02_Submenu_header_CollapsablePanelUpdatePanel>
<DIV class=menu_content_in>
<A tabIndex=-1 href="e.html">test1</A><BR>
<A tabIndex=-1 href="d.html">test2</A><BR>
<A tabIndex=-1 href="c.html">test3</A><BR></DIV></DIV>
</DIV>
</BODY>
</html>
现在,当我尝试通过硒Web驱动程序使用以下c#代码单击元素时,未单击test3
链接。它不会给我任何错误/警告。它无声地通过click方法。代码如下,
d.SwitchTo().Frame("iframe");
IWebElement e = d.FindElement(By.XPath("//*[@id='ctl00_Menu_ctl02_Submenu_header_CollapsablePanelUpdatePanel']/div/a[3]"));
e.Click();
d.SwitchTo().DefaultContent();
为什么未单击链接test3
?
有趣的事实是,当我删除以下CSS代码时,我能够自动执行点击。
#left_container_menu {
position: absolute;
overflow-x: hidden;
overflow-y: scroll;
left: 9px;
width: 190px;
top: 30px;
bottom: -3px;
}
此CSS有什么特别之处?我想使元素可点击。我怎么做?
Note:由于遗留原因,我无法更改html源代码。但是我通过c#进行了任何更改以使点击发生。任何指针都将不胜感激。
我的包裹清单,
我无法自动在Chrome / Firefox上进行点击,因为该网站仅适用于IE。我还要附加IE版本。
这是我一边使用的代码。我刚刚将您的代码添加到了我的测试代码中。
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
namespace selenium_IE_automation
{
class Program
{
private const string IE_DRIVER_PATH = @"D:\D drive backup\selenium web drivers\";
static void Main(string[] args)
{
string url = "http://localhost/a.html";
var driver = new InternetExplorerDriver(IE_DRIVER_PATH);
driver.Navigate().GoToUrl(url);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
try
{
driver.SwitchTo().Frame("iframe");
IWebElement leverancierlinkElement = driver.FindElement(By.XPath("//*[@id='ctl00_Menu_ctl02_Submenu_header_CollapsablePanelUpdatePanel']/div/a[3]"));
leverancierlinkElement.Click();
driver.SwitchTo().DefaultContent();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
输出:
编辑:
IE驱动程序服务器版本:
Selenium Web驱动程序版本: