Home > Enterprise >  How to get property of the selenium element?
How to get property of the selenium element?

Time:12-19

I am rewriting python code into java and getting confused to get translate following line:

element = driver.find_element(By.XPATH,"//*[contains(text(),'Hello')]")
id = element.get_property('attributes')[1]['textContent']

How should I convert it to Java?

CodePudding user response:

I do not think there is any method get_property present for WebElement in Java-Selenium bindings.

You can do following with getAttribute

WebElement ele = driver.findElement(By.xpath("//*[contains(text(),'Hello')]"));
String s = ele.getAttribute("textContent");
System.out.println(s);

CodePudding user response:

To rewrite the code within the question into you the following Locator Strategy:

WebElement element = driver.findElement(By.xpath("//*[contains(text(),'Hello')]"));
String id = element.getAttribute("textContent");

As an alternative, instead of getAttribute("textContent") you can also use the any of the options below:

  • String id = element.getText();
  • String id = element.getAttribute("innerHTML");
  • Related