简体   繁体   English

检查页面上是否存在元素的最佳方法是什么?

[英]What is the best way to check if element exist on page?

I have a table with cells containing data, on which i do operations. 我有一个包含数据的单元格的表,我在上面进行操作。 I am simply checking condition like: 我只是在检查条件,如:

(ExtractTextFromElement(duoqTable, duoqRowNumber, 3, "'duoqTable'").Contains("Europe") ||
  ExtractTextFromElement(duoqTable, duoqRowNumber, 3, "'duoqTable'").Contains("Russia")

The problem is that this IWebElement sometimes may disappear and if I am inside this condition block, the exception occurs. 问题是该IWebElement有时可能会消失,并且如果我在此条件块中,则会发生异常。 I could do additional check, if the element exists before every condition statement but it seems ineffective. 如果该元素在每个条件语句之前都存在,我可以进行额外的检查,但它似乎无效。 Is there any better way? 有什么更好的办法吗?

if you use find Elements method instead of find Element then you will not get exception. 如果使用find Elements方法而不是find Element,则不会出现异常。 Find Elements returns list of Web Elements you need to check the size of it. 查找元素返回您需要检查其大小的Web元素列表。

if driver.findElements(By.className("xyz").size()>0
   driver.findElements(By.className("xyz").get(0).getText();
else
   System.out.println("No element found")

The above code will not throw any exception and no need to use try/catch block. 上面的代码不会引发任何异常,也不需要使用try / catch块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM