D_ob_d
D_ob_d

Reputation: 31

Trouble getting href - Instagram automation

I am not the first and not the last one who got into this: cannot get all hrefs from instagram. Although it is common I cannot get all hrefs from a class and all solutions I tried so far desperately failed. So, would appreciate a hand or a punch into the right direction.

I am searching for a hashtag:

hashtags = '#hashtag'
search.send_keys(hashtags)
time.sleep(2)
search.send_keys(Keys.ENTER)
time.sleep(2)
search.send_keys(Keys.ENTER)

link_list=[]

links = driver.find_elements_by_class_name('Nnq7C weEfm')

for link in links:
    link_list.append(link.get_attribute('href'))
    
    print(link_list)

There are several upper level classes that select all pics by neither gives me href. I can get href from v1Nh3 kIKUG _bz0w - the class defing an individual pic on the search results page. Despite there are 33 v1Nh3 kIKUG _bz0w on the page I get only one href.

how firefox console looks like

Upvotes: 0

Views: 119

Answers (1)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

 links=[x.get_attribute("href") for x in driver.find_elements_by_xpath("//div[@class='v1Nh3 kIKUG  _bz0w']/a")]

Just use /a on the class and get the hrefs like so. I'd find a more suitable xpath since that class name looks dynamic though.

Upvotes: 1

Related Questions