0

I'm trying to return the text 'My name is' from this code. The text can change so I can't use it in the element locator.

<span class="tabComboBoxName" id="tab-ui-id-1607647" aria-hidden="true">My name is</span>

My ultimate goal is to compare this text to a variable which I have figured out, but first I need to extract the text.

Any idea on how to proceed?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Jude
  • 11
  • 4

1 Answers1

0

To extract the text My name is you can use either of the following based Locator Strategies:

  • cssSelector:

    System.out.println(driver.findElement(By.cssSelector("span.tabComboBoxName[id^='tab-ui-id-']")).getText());
    
  • xpath:

    System.out.println(driver.findElement(By.xpath("//span[@class='tabComboBoxName' and starts-with(@id, 'tab-ui-id-')]")).getText());
    

Ideally you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • xpath:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("span.tabComboBoxName[id^='tab-ui-id-']"))).getText());
    
  • xpath:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='tabComboBoxName' and starts-with(@id, 'tab-ui-id-')]"))).getText());
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352