1

I have a list of all of the people (I in this case) follow, I am trying to go through everyone's page and see if they have a link in their BIO if they do I want their account to be added to a new list. otherwise, I want to do nothing with said user.

Using xpath I have identified that users who do not have a link have this xpath /html/body/div[1]/section/main/div/header/section/div[2]/a

and users that do have a link have this xpath /html/body/div[1]/section/main/div/header/section/div[2]/a[1]

If I run driver.find_element_by_xpath("/html/body/div[1]/section/main/div/header/section/div[2]/a[1]") and the user does not have a link I should get an error as we are unable to locate the xpath.

How would I go about checking which of the two xpaths is present?

AMC
  • 2,642
  • 7
  • 13
  • 35
  • _and the user does not have a link I should get an error as we are unable to locate the xpath._ Then catch the error? What is the issue, exactly? – AMC Apr 15 '20 at 19:20
  • Does this answer your question? [Checking if element exists with Python Selenium](https://stackoverflow.com/questions/9567069/checking-if-element-exists-with-python-selenium) – AMC Apr 15 '20 at 19:21

2 Answers2

0

You could have the code:

Try:
    driver.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/div[2]/a[1]')
    #The user has a link in their bio
Except:
    #An error has been thrown and the user doesn't have a link in their bio

I'm currently building an Instagram bot and have found this to work and be useful.

Additionally, you could try:

if len(driver.find_elements_by_xpath('/html/body/div[1]/section/main/div/header/section/div[2]/a[1]')) == 0:
    #There is no link in bio
else:
    #There is a link in the bio

Peter
  • 848
  • 8
  • 14
  • Hey Pete, Yeah this worked! for i in followers: driver.get(i) try: driver.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/div[2]/a[1]') links.append(i) #The user has a link in their bio except: pass This is what I used – Christopher Gonzalez Apr 15 '20 at 18:51
  • Would something like this work to select only certain links? For example, I want only the twitter links from that new list not any other link. would you know of a way to see that? – Christopher Gonzalez Apr 15 '20 at 18:57
  • What you probably want to do, is to collect all the links and **then** sort through the array of links checking which of those are twitter links. You could do that by checking `if "twitter.com" in link: ` (assuming all the twitter links contains 'twitter.com'). – Peter Apr 15 '20 at 19:05
  • https://stackoverflow.com/questions/9567069/checking-if-element-exists-with-python-selenium – AMC Apr 15 '20 at 19:21
  • Pete, that is correct, once I can put it into a csv I can do that with pandas. But my only problem is how to extract the link – Christopher Gonzalez Apr 15 '20 at 23:13
  • ``link=driver.find_element_by_xpath('xpath to link..').text`` works for me. OR ``link=driver.find_element_by_xpath('xpath to link..').get_attribute("href")`` – Peter Apr 16 '20 at 10:38
0

Try below :

from selenium.common.exceptions import NoSuchElementException

try:
    elem = wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/section/main/div/header/section/div[2]/a[1]")))
    elem.click()
except NoSuchElementException:  
    print("Loading took too much time!")

Note : please add below imports to your solution

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
SeleniumUser
  • 4,065
  • 2
  • 7
  • 30