I am attempting to retrieve the population data from a specific element.
I have attempted to use the parent method, but encountered an error. My goal is to extract the population information for China from the table on this page:
https://en.wikipedia.org/wiki/China
Here is the code I have tried:
WebElement dateBox7 = driver.findElement(By.xpath("//*[contains(text(),'Population')]/parent::tr//td/a[text()]"));
The error I received is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
I also attempted to use the following xpath:
//*[contains(text(),'2016 estimate')]/ancestor::tr/td/text()[1]
and it successfully displayed the population in the DOM. However, when using it in Selenium, I encountered this error:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: The result of the xpath expression "//*[contains(text(),'2016 estimate')]/ancestor::tr/td/text()[1]" is: [object Text]. It should be an element.
Can anyone explain why this is not working as expected?