How to capture label text in Selenium webdriver


Script to get label text or capture label text in Selenium webdriver

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
System.out.println(driver.findElement(By.name("btnK")).getText());
System.out.println(driver.findElement(By.name("btnI")).getText());

 
}
}


Output:-

Google Search
I'm Feeling Lucky

Above scripts gets the text of buttons in google webpage.

getText() method gets text of the element.


How to get names of links in selenium webdriver?

Below script gets the links names of Signon and Register in Mercury Tours webpage.


package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
System.out.println(driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td/a")).getText());
System.out.println(driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/a")).getText());



}
}

Output:-

SIGN-ON
REGISTER

No comments:

Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge