-2

I'm trying to access click a hashtag on Instagram with Selenium, so I can see all images related to that hashtag.

The problem is, that the hashtag, which you are searching for is not always on the top... Is there a way to find and click the hashtag I am searching for?

See the problem here

Would be cool if you could help me :)

def likeByHashtag(self,hashtag):
    #click on the magnifying glass
    element = WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div/section/nav[2]/div/div/div[2]/div/div/div[2]/a")))
    element.click()
    #type in searchbar
    element = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/div/section/nav[1]/div/header/div/h1/div/div/div/div[1]/label/input")))
    element.send_keys("#" +hashtag)
    #click on #
    element = xxx
vitaliis
  • 4,082
  • 5
  • 18
  • 40

1 Answers1

2

In your specific case, the most efficient solution would be to directly navigate to the endpoint containing your hashtag. So you'd need to:

driver.get('https://www.instagram.com/explore/tags/' + hashtag) in your likeByHashtag function.

Thus:

def likeByHashtag(self,hashtag):
    driver.get('https://www.instagram.com/explore/tags/' + hashtag)

Would archive what you're looking for.

Otherwise another solution using xPath would be:

driver.find_element_by_xpath(f'//div[contains(., {hashtag})]')

Credits to @kjhughes

f-starace
  • 278
  • 1
  • 17