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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alice Polansky
  • 3,231
  • 3
  • 18
  • 25

2 Answers2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lukas M.
  • 2,859
  • 23
  • 30
  • 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** – Alice Polansky Dec 04 '13 at 12:22
  • 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( )).getAttribute("innerHTML")`; – Lukas M. Dec 04 '13 at 12:27
  • 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. – Alice Polansky Dec 04 '13 at 12:39
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ddavison
  • 28,221
  • 15
  • 85
  • 110