1

I am using Selenium-Python to Scrape the content at this link. http://targetstudy.com/school/62292/universal-academy/

HTML code is like this,

<tr>
  <td>
    <i class="fa fa-mobile">
      ::before
    </i>
  </td>
  <td>8349992220, 8349992221</td>
 </tr>

I am not sure how to get the mobile numbers in using the class="fa fa-mobile" Could someone please help. Thanks

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    from selenium.webdriver.common.action_chains import ActionChains
    import lxml.html
    from selenium.common.exceptions import NoSuchElementException

    path_to_chromedriver = 'chromedriver.exe'
    browser = webdriver.Chrome(executable_path = path_to_chromedriver)
    browser.get('http://targetstudy.com/school/62292/universal-academy/')
    stuff = browser.page_source.encode('ascii', 'ignore')
    tree = lxml.html.fromstring(stuff)
    address1 = tree.xpath('//td/i[@class="fa fa-mobile"]/parent/following-sibling/following-sibling::text()')

    print address1
Md. Mohsin
  • 1,822
  • 3
  • 19
  • 34

1 Answers1

2

You don't need lxml.html for this. Selenium is very powerful in Locating Elements.

Pass //i[@class="fa fa-mobile"]/../following-sibling::td xpath expression to find_element_by_xpath():

>>> from selenium import webdriver
>>> browser = webdriver.Firefox()
>>> browser.get('http://targetstudy.com/school/62292/universal-academy/')
>>> browser.find_element_by_xpath('//i[@class="fa fa-mobile"]/../following-sibling::td').text
u'83499*****, 83499*****'

Note, added * for not showing the real numbers here.

Here the xpath first locates the i tag with the fa fa-mobile class, then goes to the parent and gets the next following td sibling element.

Hope that helps.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I don't think you answered the question. Your code just returns the following sibling, what if there are more after? – Heinz Jun 04 '19 at 19:50