4

Is there a way to wait for a table to load completely using selenium with java? In my case it looks like the tables loaded using ajax and/or jquery. So far the only possible solution i could find it so put a simply timeout/pause in a milliseconds. I'm looking for more advanced solution and not hard-coded one. Thanks

sandeep kumar
  • 195
  • 1
  • 4
  • 20
Zvika Nadav
  • 103
  • 1
  • 8

1 Answers1

4

Selenium provides WebDriverWait with ExpectedConditions.visibilityOfElementLocated to wait for element visible on the DOM. You should try as below :-

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By...));

Note :- Here you can provide By locator as .id(),.className(), .xpath(), .cssSelector() etc.. of your table element. It will wait for 30 seconds to check visibilty of your table on the DOM.

It will throw TimeoutException exception if element will not be visible within 30 seconds.

For eg :- if you want to check visibility of your table with the id, you just run as wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("your table id")));

You can increase or decrease the timeout also..

Hope it will help you..:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • 1
    Thank you , i will try this solution. can you please just exaplin what is DOM? also what the number 30 means? is it like milliseconds/seconds of timeout for the element to be visible ? and if it won't it will throw exception?Also what is the usage of el afterwards? just i just run wait.until(ExpectedConditions.visibilityOfElementLocated(By...)); ? Thanks. – Zvika Nadav Jul 07 '16 at 12:44
  • @ZvikaNadav The Document Object Model (DOM) is a programming API for HTML and XML documents. It defines the logical structure of documents and the way a document is accessed and manipulated – Saurabh Gaur Jul 07 '16 at 12:47
  • Thank you for the info. I will try it and see if tests are not failing anymore, hopefully :) I will let you. – Zvika Nadav Jul 07 '16 at 12:50
  • @ZvikaNadav `el` is the reference variable of the `WebElement` object where you can perform certain action like `el.click()` etc... – Saurabh Gaur Jul 07 '16 at 12:50
  • @ZvikaNadav 30 is the time out limit in second.. you can increase or decrease it also.. – Saurabh Gaur Jul 07 '16 at 12:53
  • @ZvikaNadav yes it will throw `TimeoutException` if element will not be visible within 30 seconds – Saurabh Gaur Jul 07 '16 at 12:55
  • You need to run like as `wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("table id")));`...if you want to locate it with the id of the table – Saurabh Gaur Jul 07 '16 at 12:57
  • 1
    Thank you for the information . At the moment your solution solves the issue of mine, do yo know exactly what is the actual process behind the screen of the ExpectedConditions.visibilityOfElementLocated ? Could you explain about it abit more? Thanks – Zvika Nadav Jul 07 '16 at 13:13
  • @ZvikaNadav `ExpectedConditions.visibilityOfElementLocated` checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. for more details have a look http://grepcode.com/file/repo1.maven.org/maven2/org.seleniumhq.selenium/selenium-support/2.32.0/org/openqa/selenium/support/ui/ExpectedConditions.java#ExpectedConditions.visibilityOfElementLocated%28org.openqa.selenium.By%29 – Saurabh Gaur Jul 07 '16 at 13:17
  • 1
    This is great, thank you for the information and the link, very useful. Do you have any other websites like so that i can research selenium methods/functionalities ? Thanks in advance. – Zvika Nadav Jul 07 '16 at 13:20
  • @ZvikaNadav you are always welcome..I provided every points with the link in the answer.. you can click to know about on every points...keep learning...:) – Saurabh Gaur Jul 07 '16 at 13:23
  • Thanks, very appreciated. – Zvika Nadav Jul 07 '16 at 13:49
  • Hey, sadly i tried to run other test the issue still appears, if i simply run without any timeout/breakpoint, the test fails because the table is not fully loaded. any other idea maybe? – Zvika Nadav Jul 07 '16 at 14:22
  • @ZvikaNadav Why are you not using `WebDriverWait` here???..Could you share your code that have you tried so far?? – Saurabh Gaur Jul 07 '16 at 14:39
  • I am using a WD wait with a properties timeout set. it seems like the selenium sees the page loads and trying to do w/e command to get table data but it fails because it did not load fully. – Zvika Nadav Jul 09 '16 at 21:06
  • @ZvikaNadav make sure your locator to locate table is correct Then increase the timeout...:) – Saurabh Gaur Jul 10 '16 at 00:00
  • I made sure it is under , as i said the issue is where selenium is already executing the next command where the page is loaded but the table is not fully loaded(used by ajax)
    – Zvika Nadav Jul 10 '16 at 07:16
  • WebDriverWait wait = new WebDriverWait(webDriver, EnvironmentUtils.getPageLoadTimeout().intValue()); wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*/table[@id='" + tableID + "']/tbody"))); That is the code i'm running at the moment for my table methods, i changed it to presenseOfElementLocated to try it out at the moment. I can't seem to find the different between visibilityOfElementLocated and presenseOfElementLocated according to the website you gave. – Zvika Nadav Jul 10 '16 at 08:00
  • @ZvikaNadav Is there timeout exception occurs when you tried this code??? – Saurabh Gaur Jul 10 '16 at 08:15
  • Is `EnvironmentUtils.getPageLoadTimeout().intValue()` returns enough timeout for loading your table or not?? – Saurabh Gaur Jul 10 '16 at 08:19
  • Have a look for the difference between visibilityOfElementLocated and presenseOfElementLocated..http://stackoverflow.com/questions/38038920/visibilityofelementlocated-vs-presenceofelementlocated/38046692#38046692 – Saurabh Gaur Jul 10 '16 at 08:24
  • the timeout is not the issue, the issue that after the click command i verify that the table is not empty of rows but it does throw exception there, the timeout is good. thanks for sharing the answer between the methods. Also, i am sorry but i can not share the website/ fully html code. Do you mind telling me what you are looking in the html code? Also i just wanted to let you know that i asked another question here, i am trying now to use waitUntil jquery/animation/ajax is completed, i hope that i do not miss any statements there – Zvika Nadav Jul 12 '16 at 16:29
  • @ZvikaNadav i think no need to ask another question provided answer is correct to solve your problem... i want HTML for looking a best locator to wait until complete... i just want to know which element be present at last after jquery/animation/ajax is completed.. thats why locator could wait for that element until present... – Saurabh Gaur Jul 12 '16 at 16:53
  • I tried to use the Id of the table but it does not help. – Zvika Nadav Jul 12 '16 at 16:56
  • @ZvikaNadav it is hard to say which locator is best and which condition is best without seeing any live example... thanks – Saurabh Gaur Jul 12 '16 at 16:59
  • I do not understand, if I expect the table will show and load with details and the locator is not the Id of the table what else then? – Zvika Nadav Jul 12 '16 at 17:00
  • @ZvikaNadav I have already provided in the answer.. you can also use other locator like as `By.name()`,`By.className()`, `By.xpath()`, .`By.cssSelector()` etc instead of id... – Saurabh Gaur Jul 12 '16 at 17:07
  • I have tried other locator attributes for my case, it does not the case sadly. Thanks. – Zvika Nadav Jul 12 '16 at 17:08
  • @ZvikaNadav I'm sorry but I couldn't provide you exact solution without seeing your table that how it works and when it load...thanks – Saurabh Gaur Jul 12 '16 at 17:12
  • @ZvikaNadav Follow this link http://stackoverflow.com/questions/6201425/wait-for-an-ajax-call-to-complete-with-selenium-2-web-driver...may be it helps – Saurabh Gaur Jul 12 '16 at 17:22
  • Hey , if you go to [link]http://stackoverflow.com/questions/38333904/selenium-webdriver-wait-for-page-to-completely-load-in-javajavascriptajax-jq/38337184#38337184 you can see my method that i am currently using, it is the same as the guys in the link you provided are suggesting. Thanks – Zvika Nadav Jul 13 '16 at 06:40