【问题标题】:IE11 is failing to find element by using xpath while it works fine in Chrome in SeleniumIE11 无法通过使用 xpath 找到元素,而它在 Selenium 的 Chrome 中运行良好
【发布时间】: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).

试过了:

  1. 降低“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


【解决方案1】:

Xpath 因在 Internet Explorer 中无法正常工作而臭名昭著。尝试使用 CSS 选择器。另外,有必要用IE吗? Chrome 和 Firefox 最适合 Selenium。

【讨论】:

  • 是的,这是在 IE 中运行测试的要求。将尝试 CSS 选择器。
猜你喜欢
  • 1970-01-01
  • 2022-08-16
  • 1970-01-01
  • 1970-01-01
  • 2022-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多