【发布时间】:2017-09-04 16:37:00
【问题描述】:
HTML
<tr>
<td class="label" width="30%" valign="bottom">SOID:
</td>
<td class="desc" valign="top">123456789</td>
</tr>
我对值“123456789”感兴趣
Xpath
".//td[contains(@class,'label') and contains(.,'SOID:')]/following-sibling::td"
代码
public string MyMethod()
{
string soidXpath = ".//td[contains(@class,'label') and contains(.,'SOID:')]/following-sibling::td";
IWebElement soidElem = driver.FindElement(By.XPath(soidXpath));
return soidElem.Text;
}
有了这个 xpath,我的测试在 chrome 中通过了,但在 IE11 中失败了,下面是...
错误
Test 'H.Auto.RegressionSuite.TestCases.HN.HnLgoinTest.TC_Hn_login' failed: OpenQA.Selenium.InvalidSelectorException : Unable to locate an element with the xpath expression .//td[contains(@class,'label') and contains(.,'SOID:')]/following-sibling::td because of the following error:
[object Error]
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebElement.FindElement(String mechanism, String value)
at OpenQA.Selenium.Remote.RemoteWebElement.FindElementByXPath(String xpath)
at OpenQA.Selenium.By.<>c__DisplayClasse.<XPath>b__c(ISearchContext context)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at OpenQA.Selenium.Remote.RemoteWebElement.FindElement(By by)
0 passed, 1 failed, 0 skipped, took 39.33 seconds (NUnit 2.5.10).
试过了:
- 降低“Internet 选项”中的安全级别
Cannot find any elements in Selenium using Internet Explorer Driver
2.添加如下注册表(将“iexplore.exe”的值从 0 更改为 1)
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InternetExplorer\Main\FeatureControl\FEATURE_DISABLE_INTERNAL_SECURITY_MANAGER
但没有运气。任何想法或指针如何解决这个问题。提前致谢。
【问题讨论】:
-
IE 引擎有时无法解析复杂的 xpath。你能试试这个 ".//td[contains(.,'SOID:')]/following-sibling::td" 并检查一下
-
试试这个 xpath
//table//tr/td[contains(text(), 'SOID:')]/following-sibling::td[text()='123456789']注意:在执行这个 xpath 之前提供几秒钟的等待,所以你的驱动程序可能能够找到 webelement。等待使用Explicit wait方法。 -
跟踪
td class="label"并转移到following-sibling::td的任何具体要求? -
尝试更改 xpath 并添加显式等待,但结果无动于衷。是的,需要确保我获得 SOID 的值。
标签: selenium internet-explorer xpath automated-tests internet-explorer-11