1

The phone number is an element, and I need to get the inner text.

<a href="tel:895**49****" class="button-text action-link" title="Телефон продавца" rel="nofollow">
"8 9** **9-99-**"
</a>

When I use

phone = driver.find_element_by_class_name('button-text')
print phone.text

it returns an empty string, because the phone number in "" is an text() node.

And when I try

print driver.find_element_by_xpath('/html/body/section/article/section[2]/ul/li[1]/a/text()')

or this

print driver.find_element_by_xpath('/html/body/section/article/section[2]/ul/li[1]/a/text()').text

it returns this error:

InvalidSelectorException: Message: u'Error Message => 'The result of the xpath expression "/html/body/section/article/section[2]/ul/li[1]/a/text()" is: [object Text]. It should be an element.

2 Answers 2

4

You have to specify the XPath expression for returning an element, not text. Because Selenium works with elements.

  1. First locate the element (using XPath, or CSS selectors, ...)
  2. Then call the method on the element for returning its text

This should work:

print driver.find_element_by_xpath('/html/body/section/article/section[2]/ul/li[1]/a').text

Note: The XPath expression can't contain function text() (or similar) at the end, because it causes returning text and Selenium needs element(s). It is true for Selenium 2 (WebDriver) in Java.

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

3 Comments

nope, it returns empty string too, like driver.find_element_by_class_name('button-text').text i think because phone number it's a text node, not inner text in a
hmm, I am using java (not python), but if you get empty string, you can try call method getAttribute on WebElement object - something like this in java: driver.findElement(By.xpath( <some/xpath> )).getAttribute("innerHTML");
well, it should work, but it doesn't work because this site generate phone number with unknown method and put it in a new text node! and get_attribute('innerHTML') returns previous value on this button - 'Show number'. i must click on this button, wait 2 sec and get new inner text(phone number that appears after clicking) - i do this manupulations right and it works fine. driver.find_element_by_xpath('/html/body/section/article/section[2]/ul/li[1]/a').text works too and returns rigth text, but untill i click. i think it was made to protect phone numbers and i need to scrape them.
0

I'd recommend that you use the following "preferred attributes" to match, rather than classes.

The preferred attributes are:

  1. ID
  2. Name
  3. Title

Since your element has a title attribute, match on that using CSS. Save yourself the hassle and the eyesore that is, XPath.

print driver.find_element_by_css("a[title='Телефон продавца']").text

Comments

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.