Search code examples
pythonsplinter

How to get the text from an anchor tag?


I would like to get the text from all anchor tag which has class="ip_click" in the page. Here's an exactly example that I'm trying to get:

<a href="#" class="ip_click">177.1.1.1</a>
<a href="#" class="ip_click">177.2.2.2</a>

I want to get "177.1.1.1" and "177.2.2.2" and how much other

<a href="#" class="ip_click">Something here</a>

Exist in the page, so I tried:

ip = browser.find_by_css("ip_click")
for i in ip:
    print i

But the Firefox closes and nothing is displayed in terminal. Furthermore, in terminal no error is shown.


Solution

  • If you use css_selector, specify . for accessing the elements with the given classname.

    ips = browser.find_by_css(".ip_click")
    for ip in ips:
        print ip.text