Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch all href link using selenium in python

I am practicing Selenium in Python and I wanted to fetch all the links on a web page using Selenium.

For example, I want all the links in the href= property of all the <a> tags on http://psychoticelites.com/

I've written a script and it is working. But, it's giving me the object address. I've tried using the id tag to get the value, but, it doesn't work.

My current script:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


driver = webdriver.Firefox()
driver.get("http://psychoticelites.com/")

assert "Psychotic" in driver.title

continue_link = driver.find_element_by_tag_name('a')
elem = driver.find_elements_by_xpath("//*[@href]")
#x = str(continue_link)
#print(continue_link)
print(elem)
like image 402
Xonshiz Avatar asked Jan 13 '16 06:01

Xonshiz


People also ask

How do you select a link in Selenium Python?

We can click on a link using Selenium webdriver in Python. A link is represented by the anchor tag. A link can be identified with the help of the locators like - link text and partial link text. We can use the link text attribute for an element for its identification and utilize the method find_element_by_link_text.

Where can I find HREF in Selenium?

We can fetch href links in a page in Selenium by using the method find_elements(). All the links in the webpage are designed in a html document such that they are enclosed within the anchor tag. To fetch all the elements having <anchor> tagname, we shall use the method find_elements_by_tag_name().


Video Answer


3 Answers

Well, you have to simply loop through the list:

elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
    print(elem.get_attribute("href"))

find_elements_by_* returns a list of elements (note the spelling of 'elements'). Loop through the list, take each element and fetch the required attribute value you want from it (in this case href).

like image 61
JRodDynamite Avatar answered Oct 17 '22 20:10

JRodDynamite


I have checked and tested that there is a function named find_elements_by_tag_name() you can use. This example works fine for me.

elems = driver.find_elements_by_tag_name('a')
    for elem in elems:
        href = elem.get_attribute('href')
        if href is not None:
            print(href)
like image 45
Gabriel Chung Avatar answered Oct 17 '22 20:10

Gabriel Chung


You can try something like:

    links = driver.find_elements_by_partial_link_text('')
like image 3
Shawn Avatar answered Oct 17 '22 22:10

Shawn