【问题标题】:Selenium c# cannot locate element with textSelenium c#无法用文本定位元素
【发布时间】:2015-12-04 21:08:39
【问题描述】:

我是 C# 中的 Selenium 新手。我尝试使用

wait.Until(ExpectedConditions.ElementIsVisible(By.Id("someId")));
new SelectElement(driver.FindElement(By.Id("someId"))).SelectByText("someText");

我的 NUnit 输出中出现这样的错误:

OpenQA.Selenium.NoSuchElementException : 无法定位元素 文本:一些文本

但是当我将 wait.Unitil 语句替换为 Tread.Sleep(3000) 时,我的测试可以顺利通过。

需要帮助。请指教。

【问题讨论】:

  • 在尝试访问元素之前,很可能没有足够的时间将元素加载到 DOM 中。
  • @ChrisHawkes 你的意思是加载到 DOM 中的文本元素吗?我的意思是为什么 wait.Until 不起作用。
  • 看看我在这篇文章中的回答:stackoverflow.com/questions/23084078/…

标签: c# selenium-webdriver nunit


【解决方案1】:

它会起作用的:

wait.Until(d => d.FindElement(By.XPath("//*[@id='someId']//*[text()='someText']")));
new SelectElement(driver.FindElement(By.Id("someId"))).SelectByText("someText");

但最好将其重构为单个直到,例如:

wait.Until(d =>
{
new SelectElement(d.FindElement(By.Id("someId"))).SelectByText("someText");
return d;
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-10
    • 2022-01-06
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多