Any one can send me sample code how to verify element
in Selenium WebDrvier using Java
isDisplayed() is the method used to verify a presence of a web element within the webpage. The method returns a “true” value if the specified web element is present on the web page and a “false” value if the web element is not present on the web page.
I used java print statements for easy understanding.
To check Element Present:
if(driver.findElements(By.xpath("value")).size() != 0){ System.out.println("Element is Present"); }else{ System.out.println("Element is Absent"); }
Or
if(driver.findElement(By.xpath("value"))!= null){ System.out.println("Element is Present"); }else{ System.out.println("Element is Absent"); }
To check Visible:
if( driver.findElement(By.cssSelector("a > font")).isDisplayed()){ System.out.println("Element is Visible"); }else{ System.out.println("Element is InVisible"); }
To check Enable:
if( driver.findElement(By.cssSelector("a > font")).isEnabled()){ System.out.println("Element is Enable"); }else{ System.out.println("Element is Disabled"); }
To check text present
if(driver.getPageSource().contains("Text to check")){ System.out.println("Text is present"); }else{ System.out.println("Text is absent"); }
You could try something like:
WebElement rxBtn = driver.findElement(By.className("icon-rx")); WebElement otcBtn = driver.findElement(By.className("icon-otc")); WebElement herbBtn = driver.findElement(By.className("icon-herb")); Assert.assertEquals(true, rxBtn.isDisplayed()); Assert.assertEquals(true, otcBtn.isDisplayed()); Assert.assertEquals(true, herbBtn.isDisplayed());
This is just an example. Basically you declare and define the WebElement variables you wish to use and then Assert
whether or not they are displayed. This is using TestNG Assertions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With