-1

I want to get a text which is inside a div. I tried to find help from other questions, but most of them say .getText() which doesnt exist anymore.

This is the inspect: enter image description here

Im using a chrome extension with which i can copy out the XPath, but it doesnt reads the text inside the div. It copies me this here:

//div[@class='mud-alert-message']
ddavison
  • 28,221
  • 15
  • 85
  • 110
Beardy
  • 163
  • 1
  • 15
  • Does this answer your question? [How to get innertext of a DIV using javascript?](https://stackoverflow.com/questions/15903993/how-to-get-innertext-of-a-div-using-javascript) – T McKeown Nov 30 '21 at 19:52
  • The problem is that you've tagged this question as Java but you are using C#. That's why `.getText()` doesn't exist. You need `driver.FindElement(...).Text;`. – JeffC Nov 30 '21 at 20:31
  • Does this answer your question? [C# Selenium WebDriver get text from label](https://stackoverflow.com/questions/36580442/c-sharp-selenium-webdriver-get-text-from-label) – JeffC Nov 30 '21 at 20:32

2 Answers2

2

To print the text Die Datei... you can use either of the following Locator Strategies:

  • Using cssSelector and getAttribute("innerHTML"):

    System.out.println(wd.findElement(By.cssSelector("div.mud-alert-message")).getAttribute("innerHTML"));
    
  • Using xpath and getText():

    System.out.println(wd.findElement(By.xpath("//div[@class='mud-alert-message']")).getText());
    

Ideally, to extract the text you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • Using cssSelector and getText():

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.mud-alert-message"))).getText());
    
  • Using xpath and getAttribute("innerHTML"):

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='mud-alert-message']"))).getAttribute("innerHTML"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • getText() doesnt work at xpath. It has a red underline that getText() doesnt exist – Beardy Nov 30 '21 at 20:05
  • Oh but this works `IWebElement test = webDriver.FindElement(By.XPath("//div[@class='mud-alert-message']")); string text = test.GetAttribute("innerHTML");` Thanks!!! – Beardy Nov 30 '21 at 20:06
  • @Beardy `getText()` is a method of the `IWebElement` interface. You must obtain an instance of `WebElement` in order to have access to this method. – hfontanez Nov 30 '21 at 20:08
  • @Beardy Your question tagged with _Java_ and possibly you mentioned about `getText()` that's why the answer was in _java_. Had your question being tagged with `C#`, I would have suggested `.Text` i.e. the text attribute or `GetAttribute("innerHTML")` – undetected Selenium Nov 30 '21 at 20:17
-1

I believe is something like this:

WebElement element = new WebDriverWait(driver, ...).until(ExpectedConditions.elementToBeXXX(By.LOCATOR_TYPE));
String text = element.getText();
hfontanez
  • 5,774
  • 2
  • 25
  • 37
  • It says "Non-Invocable member "IWebElement.text" cannot be used like a method. Im using it in C# – Beardy Nov 30 '21 at 20:02
  • I think it is `getText()` – hfontanez Nov 30 '21 at 20:03
  • @Beardy it is `getText()` and the API has not changed as far as I can see. This method should be available. I am not sure what you saw or where you saw this problem. But the method is still valid. – hfontanez Nov 30 '21 at 20:06