0

I am new to Selenium and I have been having a problem, I am trying to extract the the title "ferric maltol" as string, here:

https://meshb.nlm.nih.gov/record/ui?ui=C062088

But I have been having lots of troubles with this.

Using the xpath in Python with several different attempts, but none work:

  1. First attempt
to_store=driver.find_elements_by_xpath('/html/body/div[2]/h1/text()').get_attribute('textContent')

*** selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "/html/body/div[2]/h1/text()" is: [object Text]. It should be an element.

  1. Second attempt
to_store=driver.find_elements_by_xpath('/html/body/div[2]/h1/text()').text

*** selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "/html/body/div[2]/h1/text()" is: [object Text]. It should be an element.

  1. Third attempt
to_store=driver.find_elements_by_xpath('/html/body/div[2]/h1/text()').get_attribute('outerHTML')

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "/html/body/div[2]/h1/text()" is: [object Text]. It should be an element.

Does anyone have a clue?

2
  • Do see my answer and let me know if it helps. Commented May 13, 2020 at 3:07
  • In this particular case even //h1 xpath selector should work: to_store=driver.find_element_by_xpath("//h1").text Commented May 13, 2020 at 4:54

2 Answers 2

2

As your XPath expression terminates with text(), this resolves to a text container, and not an HTML element (or list of). You therefore, get the above error.

driver = webdriver.Chrome('../chromedriver.exe') #set your path here
driver.get("https://meshb.nlm.nih.gov/record/ui?ui=C062088")
driver.set_page_load_timeout(45)
driver.implicitly_wait(2)
SpecialPrice=driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div/dl/dd[1]").text
print(SpecialPrice)

Outputs

ferric maltol

Using the above method you pass in the full xpath and use the text attribute to get the value.

Sign up to request clarification or add additional context in comments.

3 Comments

Uoooooou! I see my mistake! thaanks so muuch! It worked really well, I also did not thought i would need a timeout and a wait. Thaaaanks it really helped!
Same issue for me. I am trying to get "Snowden-Pencer" from accessgudid.nlm.nih.gov/devices/10885403151217. Can you help with the xPath here?
i need to see your code and see where you are going wrong. I would recommend asking it as a question and reply the link here so i can take a look at it.
0

Selenium won't allow you to locate an element using Text node. As an alternative you can use below javascript code to extract text

element = driver.find_element_by_css_selector('h1.ng-binding.ng-scope')
text = driver.execute_script("return arguments[0].childNodes[0].textContent", element);
print(text.strip())

Output:

ferric maltol

1 Comment

Thaaanks soo much! It fit like a glooove! I have to learn Java! It looks really handy! Thanks again i was pretty hopeless

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.