AnOldSoul
AnOldSoul

Reputation: 4207

How to get Text from a div element returns just a "." with Selenium

I have a div element which looks like below.

enter image description here

I identify this element with the following xpath.

//*[contains(@class,'ce-component-title')]

Selenium identifies this element and loads the WebElement object. But when I go to get its text, I'm just getting a "." as shown below instead of getting "Purchase to pay process". What am I doing wrong here? I checked the chrome console and there's no other element matching this xpath.

enter image description here

Any help would be much appreciated.

Upvotes: 0

Views: 133

Answers (2)

Dmitry Shyshkin
Dmitry Shyshkin

Reputation: 384

I think your xpath (//*[contains(@class,'ce-component-title')]) returns more than one element, and the element you need is not the first one. Try using console of your browser, to verify how many elements this xpath returns. I can help you if you give me url to this page.

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193348

As per the HTML you have shared the element is an Angular element so you have to induce WebDriverWait for the desired element to be visible and you can use either of the following solutions:

  • cssSelector:

    String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[ng-if*='getTitle']>div.ce-component-title"))).getAttribute("innerHTML");
    
  • xpath:

    String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(@ng-if,'getTitle')]/div[@class='ce-component-title flex text-overflow ng-binding']"))).getAttribute("innerHTML");
    

Upvotes: 1

Related Questions