【发布时间】:2014-12-04 23:00:30
【问题描述】:
我看到一个奇怪的失败,无法选择一对 jQuery ui 选项卡中的第二个选项卡。这适用于 ChromeDriver,但适用于 PhantomJSDriver。
我的 PhantomJS 版本:1.9.8
jQuery 版本:1.10.2
jQuery ui 版本:1.10.3
IDE:VS2012
你可以在这里找到我的页面的副本:http://jsfiddle.net/anjw2gnr/1/
这是页面的相关部分:
<div id="tabs">
<ul>
<li>
<a href="#tabs-1">Tab 1</a>
</li>
<li>
<a href="#tabs-2">Tab 2</a>
</li>
</ul>
<div id="tabs-1">
<button id="tabOneBtn">I'm in tab one</button>
<p id="tabOneCount"></p>
</div>
<div id="tabs-2">
<button id="tabTwoBtn">I'm in tab two</button>
<P id="tabTwoCount"></P>
</div>
</div>
这是我的单元测试的样子:
[TestMethod, TestCategory("SampleTest")]
public void SampleJQueryTabsTest()
{
driver.FindElement(By.XPath("//div[@id='tabs']/ul/li[1]")).Click();
driver.FindElement(By.Id("tabOneBtn")).Click();
// assert that count is now 1
Assert.AreEqual("1", driver.FindElement(By.Id("tabOneCount")).Text);
// Click the second tab
driver.FindElement(By.XPath("//div[@id='tabs']/ul/li[2]")).Click();
driver.FindElement(By.Id("tabTwoBtn")).Click();
// assert that count is now 1
Assert.AreEqual("1", driver.FindElement(By.Id("tabTwoCount")).Text);
}
使用 ChromeDriver 运行,一切顺利。但是,当我使用 PhantomJSDriver 运行时,它会在以下行中失败:
driver.FindElement(By.Id("tabTwoBtn")).Click();
结果消息:
测试方法 MyProject.WebDriverDemo.SampleJQueryTabsTest 抛出异常: OpenQA.Selenium.ElementNotVisibleException: {"errorMessage":"元素当前不可见,可能无法操作","request":{"headers":{"Accept":"application/json, image/png","Connection ":"关闭","Content-Length":"0","Content-Type":"application/json;charset=utf-8","Host":"localhost:49593"},"httpVersion":" 1.1","method":"POST","post":"","url":"/click","urlParsed":{"anchor":"","query":"","file": "单击","目录":"/","路径":"/click","相对":"/click","端口":"","主机":"","密码":"" ,"user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":[ "click"]},"urlOriginal":"/session/e22c43f0-7c05-11e4-9c9e-6191347cc85b/element/%3Awdc%3A1417732544176/click"}}
不可见的相关元素是选项卡 2 下的按钮,但按钮不可见的唯一原因是选项卡 2 从未被单击。这意味着以下行失败,但仅使用 PhantomJSDriver:
driver.FindElement(By.XPath("//div[@id='tabs']/ul/li[2]")).Click();
关于为什么这只会对 PhantomJSDriver 而不是 ChromeDriver 失败有什么想法吗?这是 PhantomJS 中可能存在的错误吗?
附加说明:
当我在 VS2012 中将下面这行放入 Quick Watch 中时:
driver.FindElement(By.XPath("//div[@id='tabs']/ul/li[2]"))
对于 Selected 属性,我看到以下内容:
driver.FindElement(By.XPath("//div[@id='tabs']/ul/li[2]")).Selected' 抛出类型为 'OpenQA.Selenium.InvalidElementStateException' 的异常 bool { OpenQA.Selenium.InvalidElementStateException}
但是,当使用 ChromeDriver 运行时,Selected 属性仅显示值为 false 而不是 InvalidElementStateException。
【问题讨论】:
-
您好,我尝试更改窗口大小,但没有解决问题。我认为这个问题的根本原因是由于某种原因,PhantomJSDriver 不会让我单击选项卡,因为元素处于无效状态。但是,这种无效状态只出现在 PhantomJSDriver 中。
-
您能否将
FindElement与Click分开来确定是哪个操作导致了它?您是否尝试过点击.../li[2]/a而不是.../li[2]? -
是的。就是这样!我不敢相信它失败了,只是因为我没有在我的 XPath 末尾添加“/a”。哇。谢谢你的帮助!但是 ChromeDriver 接受没有“/a”的 XPath 仍然是一个非常奇怪的问题,但 PhantomJSDriver 很挑剔。
标签: .net selenium-webdriver phantomjs jquery-ui-tabs ghostdriver