1

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?

2 Answers2

1

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);
cruisepandey
  • 26,802
  • 6
  • 16
  • 37
0

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");
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281