简体   繁体   English

Selenium [Java]:如何让它等到表被刷新?

[英]Selenium [Java] : How can I make it wait until a table has been refreshed?

Assume that I have a web page that has records, displayed in a table ( that has several columns).假设我有一个 web 页面,该页面有记录,显示在表格中(有几列)。 Now there is search-input-box and a search-button .现在有search-input-boxsearch-button Using those I am searching for records with an id in the table .使用那些我在 table 中搜索具有 id 的记录

I am using implicit wait which seems to be working fine in most of the cases and failing under this scenario:我正在使用隐式等待,这在大多数情况下似乎都可以正常工作,但在这种情况下会失败:

  • I made the page to load all the data in the table after I logged in using implicit wait我使用隐式等待登录后,使页面加载表中的所有数据
  • Entered id in the search-input-box and clicked search-button在搜索输入框中输入 id 并单击搜索按钮
  • Now I am expecting only one record instead of all the records that were displayed in the table before the search happened现在我期望只有一条记录,而不是搜索发生之前表中显示的所有记录

It seems that it did not find enough time to refresh the table and still showing all the records even though I do search with ( for the id columns in the table):似乎它没有找到足够的时间来刷新表并仍然显示所有记录,即使我使用(对于表中的 id 列)进行搜索:

List<WebElement> idRows = driver.wait(<waittime>, <interval>).until(driver -> driver.findElement(By.id("column-id")));

If I use Thread.sleep() after clicking the search-button, everything is fine as I am seeing the table has been refreshed with the expected result.如果我在单击搜索按钮后使用Thread.sleep() ,那么一切都很好,因为我看到表格已经刷新了预期的结果。 However, I have to avoid using thread.sleep.但是,我必须避免使用 thread.sleep。

May I get any suggestion on how can I make it wait until the table has been refreshed with searched data?我可以就如何让它等到用搜索到的数据刷新表得到任何建议吗?

If you know that the id is unique which you are searching, in that case, you can do like this:如果您知道要搜索的 id 是唯一的,在这种情况下,您可以这样做:

After clicking the search button wait for records size be 1单击search按钮后,等待记录大小为 1

wait.until(driver -> records.size() == 1);

In case when your searched id is not unique and after searching you can see multiple results, you can do it this way:如果您搜索的id不是唯一的并且搜索后您可以看到多个结果,您可以这样做:

Before clicking the search button you can get the size of records在单击search按钮之前,您可以获得记录的大小

int size = records.size();

After clicking the search button you can wait for records size be increased:单击search按钮后,您可以等待记录大小增加:

wait.until(driver -> records.size() < size);

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

相关问题 如何使Java程序暂停直到按下特定按钮? - How can I make my Java program pause until a specific button has been pressed? 我如何等待,直到RabbitMQ消息已同步到集群中的其他节点? - How can i wait until a RabbitMQ message has been synced to the other nodes in the cluster? 我无法让程序等到GUI完成收集所需信息(Java) - I can't make my program to wait until GUI has finished gathering requested info (Java) 使应用程序等到定位服务已打开? - Make the application wait until the location services has been turned on? 如何在单击按钮之前隐藏jlist? - How can I hide a jlist until a button has been clicked? Java如何等待命令完成? - Java how to wait until a command has completed? Java - 如何等到uploadFileToBlockBlob完成? - Java - How to wait until uploadFileToBlockBlob has finished? (Java)如何使数据库删除存储的值,直到该值已存储5次以上? - (Java) How do I make a database delete a stored value until the value has been stored more than 5 times? 如何让 selenium(Chrome) webdriver 等到网络中没有待处理的请求? (使用JAVA) - How to make selenium(Chrome) webdriver wait until there is no pending request in the network? (Using JAVA) 我怎么能等到组件以Java显示? - How can I wait until a component is shown in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM