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;"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