I am trying to extract the population figure/text of an element.
I tried with parent but its showing error. I am trying to get the Population of China from this page table https://en.wikipedia.org/wiki/China
Code trial:
WebElement dateBox7 = driver.findElement(By.xpath("//*[contains(text(),'Population')]/parent::tr//td/a[text()]"));
Error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
I have tried //*[contains(text(),'2016 estimate')]/ancestor::tr/td/text()[1]
this xpath it's showing the population in DOM.But in selenium it's showing 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.
Why it's not working?