How To Get Text Of An Element In Selenium?

How can I get the text of an element using the getText() method? How can I fetch text from a webpage that might be present in any of the web-element?

1 Like

Please share the application and the element from which you want to get the text. or at least div

In general, Selenium has getText() method

// option 1
String text = driver.findElement(by).getText();

// option 2
public String getTextByJS(WebElement element){
String text="";
JavascriptExecutor executor = (JavascriptExecutor) driver;
logger.info("Clicking Locator by using JScript "+ element);
text=(String) executor.executeScript(β€œreturn arguments[0].innerHTML;”, element);
return text;
}

Are you talking about the contains(text() , ) XPATH locator?
my python code like so

driver.find_element_by(XPATH, β€œ//b[contains(text(), β€˜Conclusion’)]”)

to return any bold element that contains the word Conclusion