GVKun编程网logo

避免Selenium中出现NoSuchElementException的最佳方法是什么?(如何避免selenium被识别出来)

10

对于避免Selenium中出现NoSuchElementException的最佳方法是什么?感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解如何避免selenium被识别出来,并且为您提供关

对于避免Selenium中出现NoSuchElementException的最佳方法是什么?感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解如何避免selenium被识别出来,并且为您提供关于c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用、java – 在Selenium中避免NoSuchElementException的最好方法是什么?、Java中的NoSuchElementException异常常见原因是什么?、OpenQA.Selenium.NoSuchElementException:没有这样的元素:无法定位元素 C# 按类查找的宝贵知识。

本文目录一览:

避免Selenium中出现NoSuchElementException的最佳方法是什么?(如何避免selenium被识别出来)

避免Selenium中出现NoSuchElementException的最佳方法是什么?(如何避免selenium被识别出来)

我使用Java在Selenium
WebDriver中编写了一些测试用例,并在网格(集线器和多个节点)上执行它们。我注意到有一些测试案例由于导致失败NoSuchElementException。避免NoSuchElementException并确保始终找到该元素的最佳且可靠的方法是什么?

答案1

小编典典

您永远无法确定会找到该元素,实际上这是功能测试的目的-
告诉您页面上是否有任何更改。但有一两件事肯定是有帮助是添加等待这往往导致元素NoSuchElementException

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用

c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用

我们有一个.NET 4.5,MVC,C#项目.我们正在使用Selenium进行UI测试,测试会在我们有wait.Until()的行上间歇性地失败.一个这样的例子是:

NoSuchElementException was unhandled by user code
An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user code
Additional information: Unable to locate element: {"method":"css selector","selector":"#assessment-472 .status-PushedToSEAS"}

它就在这里抛出:

Thread.Sleep(600);
wait.Until(drv => drv.FindElement(By.CssSelector("#" + assessmentQueueId + " .status-PushedToSEAS")));

我可以看到浏览器打开,我看到它到达那一点,我可以检查元素以查看元素是否存在.它的id完全正确.

我们有这个问题很多,到目前为止解决方案是在它前面抛出Thread.Sleep(600)(或类似的时间). wait.Until()的重点是不必这样做,这使我们的测试套件变得非常长.另外,正如您在上面的示例中所看到的,有时我们甚至在将Thread.Sleep()放在它前面之后也会遇到问题,我们必须延长时间.

为什么.NET Selenium的WebDriver.Until()不起作用,并且有一种不同的方式来做同样的事情而不需要等待一段时间?同样,问题是间歇性的,这意味着它有时只会发生,它可能发生在任何数量的wait.Until()语句中,而不仅仅是显示的那个!

编辑:

这是一个类变量.
私人WebDriver等等;

它实例化如下:

this.wait = new webdriverwait(driver,TimeSpan.FromSeconds(10));

解决方法

我决定不使用webdriverwait.Until(),而是在主驱动程序上使用隐式等待:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

为了给我这个想法,我在一个不同的问题上充分赞扬了this answer.

java – 在Selenium中避免NoSuchElementException的最好方法是什么?

java – 在Selenium中避免NoSuchElementException的最好方法是什么?

我在Selenium WebDriver中使用 Java编写了几个测试用例,并在网格(集线器和多个节点)上执行它们.我注意到,由于NoSuchElementException,几个测试用例失败.什么是避免NoSuchElementException和确保元素总是找到最好和最强大的方法?

解决方法

你永远不能确定该元素将被发现,实际上这是功能测试的目的 – 告诉你页面上是否有任何改变.但有一点肯定有帮助的是添加等待通常导致NoSuchElementException的元素
webdriverwait wait = new webdriverwait(webDriver,timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

Java中的NoSuchElementException异常常见原因是什么?

Java中的NoSuchElementException异常常见原因是什么?

java是一种很常见的编程语言,使用广泛。在java编程中,开发者可能会遇到nosuchelementexception异常。nosuchelementexception异常指的是在使用scanner类从输入流中读取数据时,如果输入流中不再有 token 可用,而又试图调用next方法,就会抛出该异常。下面是nosuchelementexception异常常见的原因:

  1. 输入流中没有足够的token可用:当调用Scanner类的next()或nextXXX()方法时,要求输入流中必须有对应类型的下一个token。如果输入流中没有足够的token,则会抛出NoSuchElementException异常。
  2. 没有使用hasNext()或hasNextXXX()方法进行检查:在调用nextXXX()方法之前,必须首先调用hasNextXXX()方法进行检查。否则,如果没有可用的token,会抛出NoSuchElementException异常。
  3. 格式不正确:如果输入流中的数据格式不正确,就会抛出NoSuchElementException异常。在使用Scanner类从输入流中读取数据时,如果遇到格式不正确的数据,Scanner类会自动抛出NoSuchElementException异常。
  4. 输入流已结束:当输入流结束时,如果继续调用next()或nextXXX()方法,就会抛出NoSuchElementException异常。
  5. 输入流中包含非法字符:如果输入流中包含非法字符,Scanner类将会自动抛出NoSuchElementException异常。

在开发Java程序时,遇到NoSuchElementException异常,开发者需要检查代码,确保输入流中包含足够的token,并使用hasNext()或hasNextXXX()方法进行检查,以及正确处理格式和非法字符。

以上就是Java中的NoSuchElementException异常常见原因是什么?的详细内容,更多请关注php中文网其它相关文章!

OpenQA.Selenium.NoSuchElementException:没有这样的元素:无法定位元素 C# 按类查找

OpenQA.Selenium.NoSuchElementException:没有这样的元素:无法定位元素 C# 按类查找

如何解决OpenQA.Selenium.NoSuchElementException:没有这样的元素:无法定位元素 C# 按类查找?

Message: 
Test method CFSSOnlineBanking.SmokeTests.Transferdisplay threw exception: 
OpenQA.Selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#menuLeftRailContainer > div:nth-child(1) > div:nth-child(3)"}
  (Session info: chrome=91.0.4472.164)

Stack Trace: 
RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
RemoteWebDriver.Execute(String driverCommandToExecute,Dictionary`2 parameters)
RemoteWebDriver.FindElement(String mechanism,String value)
RemoteWebDriver.FindElementByCssSelector(String cssSelector)
<>c__displayClass23_0.<CssSelector>b__0(ISearchContext context)
By.FindElement(ISearchContext context)
RemoteWebDriver.FindElement(By by)
TransferPage.get_TransferLink() line 22
SmokeTests.Transferdisplay() line 57

我目前正在尝试单击位于 div 内的 href。无论我尝试什么,似乎都无法点击它。

enter image description here

这是我检查时的元素。

enter image description here

我尝试添加等待 我尝试通过 LinkText、PartialLinkText、cssSelector 和 xPATH 单击

目前我在 TransferPage 类中有这个:

public IWebElement TransferLink => _driver.FindElement(By.CssSelector("#menuLeftRailContainer > div:nth-child(1) > div:nth-child(3)"));

然后在我的实际测试方法中:

_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
transferPage.TransferLink.Click();

解决方法

您可以尝试以下方法吗

xpath :

//a[@href=''/connect-native/transfer/'']

或 css 选择器

a[href=''/connect-native/transfer/''] 

在代码中:

public IWebElement TransferLink => _driver.FindElement(By.CssSelector("a[href=''/connect-native/transfer/'']"));

因为您在代码中使用的 css 选择器看起来有点乱。你确定有 1/1 匹配的元素吗?

如果这不起作用,您能在您的网页中查找 iframe 吗?

我们今天的关于避免Selenium中出现NoSuchElementException的最佳方法是什么?如何避免selenium被识别出来的分享已经告一段落,感谢您的关注,如果您想了解更多关于c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用、java – 在Selenium中避免NoSuchElementException的最好方法是什么?、Java中的NoSuchElementException异常常见原因是什么?、OpenQA.Selenium.NoSuchElementException:没有这样的元素:无法定位元素 C# 按类查找的相关信息,请在本站查询。

本文标签: