Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webdriver - wait for text of an element to change

Tags:

java

selenium

I created an object for an element in my application. This element is text for records displayed on the screen. So on the first page, it will say something like "displaying 1-10 of 2100"

public static final String VTeam_M_Detail_VRecordsText = "css=#UserList:last-of-type > div:last-child";

When I am changing pages, the text of that element changes. So if I go to the 2nd page, it will say "displaying 11-20 of 2100". How can I get webdriver to wait for the text of that element to change before continuing the test. So when I go from page 1 to page 2, I want to wait for that element to have text equal to "displaying 11-20 of 2100" before continuing with the rest of the test.

like image 667
TestRaptor Avatar asked Nov 14 '14 16:11

TestRaptor


People also ask

How do you wait for text in Selenium?

You can program Selenium to wait for an element to be displayed, to change, or wait for specific text. When applications load elements or update the text of elements dynamically based on behavior or time, you can wait for text in Selenium until that change happens on the web page.

How do you make Selenium wait for an element to appear?

Selenium: Waiting Until the Element Is Visiblevar wait = new WebDriverWait(driver, TimeSpan. FromSeconds(20)); As you can see, we give the WebDriverWait object two parameters: the driver itself and a TimeSpan object that represents the timeout for trying to locate the element.

How do I make Selenium wait 10 seconds?

We can make Selenium wait for 10 seconds. This can be done by using the Thread. sleep method. Here, the wait time (10 seconds) is passed as a parameter to the method.

How do I make Selenium wait a few seconds?

How can I ask the Selenium-WebDriver to wait for few seconds in Java? To tell the Selenium WebDriver to wait for a specified amount of time use Thread. sleep. This will tell Java to pause execution of the test for the specified number of milliseconds.


2 Answers

Each time, before you change page, retrieve the text, i.e, the number of records. Then, Click on the Next button to navigate to the next page. And then, wait for that to be invisible using explicit wait.

Below code might help you out:

String retrieved_text = driver.findElement(By.xpath("//xpath of the element related to records")).getText(); //Retrieving text, i.e., the innerHtml representing number of records

/*Click on the next button to navigate to the next page*/
driver.findElement(By.xpath("//next button's xpath")).click();

WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.invisibilityOfElementWithText(By.xpath("//xpath of the element related to records"), retrieved_text)); 

NOTE: In the last line of code use xpath that locates the element that shows text 'displaying 1-10 of 2100', i.e, the element that shows display per page information and use the retrieve text to infer that the concerned element with that certain text is invisible or not in the next page.

This will help in identifying the invisibility of the concerned element easily.

OTHER WAY: In case you want to navigate to next pages till you detect a certain element

(PUT EVERYTHING IN A DO-WHILE LOOP)

  • Wait for the element to appear in the page.
  • If found, then write code to act on the element and break out of loop.
  • Else, click on the Next button
  • CONDITION will be till the NEXT button gets disabled.
like image 187
Subh Avatar answered Oct 07 '22 02:10

Subh


I needed to do the same thing and found a solution.. You can use WebDriverWait along with the ExpectedCondition.not to wait until text changes.

WebElement textElement = driver.findElementByCssSelector("#UserList:last-of-type > div:last-child"));
String currentText = textElement.getText();

//poll every .5 seconds for 5 seconds until condition is met
WebDriverWait wait = new WebDriverWait(driver, 5);
//wait until text changes
wait.until(ExpectedConditions.not(ExpectedConditions.textToBePresentInElement(countdownTime,"5:00")));
like image 25
Michael Stalcup Avatar answered Oct 07 '22 02:10

Michael Stalcup