Normalize-space is very useful function in XPath when you build it with reference of some string or keyword to use it webdriver script and it has leading or trailing intermediate repeating white space. Normalize-space will strip such unwanted space or white space. In selenium webdriver, very often we use keywords reference in building xpath. If there is not any good reference to build xapth then it is mandatory to use such keywords as reference.
You can look at my post describing different ways to build xpath .
Here i am presenting one example where we will use normalize-space function to build xpath and use it in selenium webdriver test script.
Look at above image. You can see that label France contain leading and trailing space. Now if you wants to use keyword France in xpath building, You must need to strip space. Otherwise it wan't work for you. So here, Normalize-space function will help you to strip leading and trailing space.
Complete webdriver script demonstrate usage of normalize-space is as below.
package test;import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class basic { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); driver.get("https://only-testing-blog.blogspot.com/2014/01/textbox.html"); driver.findElement(By.xpath("//*[@id=\"check3\"]")).click(); //Used Normalize-space to strip leading and trailing space. driver.findElement(By.xpath("//*[normalize-space(text())='France']")).click(); //working copy } }
Here you can see, normalize-space function is used in xpath building to strip leading and trailing space.
No comments:
Post a Comment