Python Forum
Click Element if displayed using Selenium and Python
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Click Element if displayed using Selenium and Python
#1
Hi guys, I'm coding a script in which, when a button is clicked, sometimes a div appears and others it doesn't.
I'm trying to handle this exception using the following code

for i, j  in zip(range(160), tag):

    if i%10  == 0:
      url_to_search = "https://www.instagram.com/explore/tags/" + j + "/"
      driver.get(url_to_search)
      element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".v1Nh3.kIKUG._bz0w")))
      element.click() #Click prima foto

      element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".oW_lN._0mzm-.sqdOP.yWX7d")))
      element.click() #Click follow
      
      checkFo = driver.find_element_by_class_name(".aOOlW.HoLwm")

      if checkFo.is_displayed():
          checkFo.click
          
      else:
          element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".HBoOv.coreSpriteRightPaginationArrow")))
          element.click() #Click arrow

    else:
          element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".oW_lN._0mzm-.sqdOP.yWX7d")))
          element.click() #Click follow
          
          if checkFo.is_displayed():
              checkFo.click
          
          else:
              element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".HBoOv.coreSpriteRightPaginationArrow")))
              element.click() #Click arrow
              
          time.sleep(3)
But it doesn't work! The shell gives me this error:
Unable to locate element: {"method":"class name","selector":".aOOlW.HoLwm"}
Can anybody help me?
Reply
#2
You can try waiting to make sure that element is up before, or try an ID instead. You wait for the others, but are not waiting for the one that is giving you an error.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Click on a button on web page using Selenium Pavel_47 7 4,507 Jan-05-2023, 04:20 AM
Last Post: ellapurnellrt
  Selenium suddenly fails to find element Pavel_47 3 6,151 Sep-04-2022, 11:06 AM
Last Post: Pavel_47
  Show HTML in Python application and handle click SamHobbs 2 2,646 Sep-28-2021, 06:27 PM
Last Post: SamHobbs
  Unable to click element in web page Kalpana 0 1,803 Jun-25-2020, 05:20 AM
Last Post: Kalpana
  Log In Button Won't Click - Python Selenium Webdriver samlee916 2 3,755 Jun-07-2020, 04:42 PM
Last Post: samlee916
  Hyperlink Click is not working in Selenium webdriver rajeev1729 0 1,980 May-02-2020, 11:21 AM
Last Post: rajeev1729
  selenium click in iframe fails 3Pinter 6 4,972 Apr-29-2020, 12:59 PM
Last Post: Larz60+
  find element...click() dont work windows11 6 3,051 Apr-23-2020, 11:13 PM
Last Post: law
  bypassing find element click() windows11 1 1,816 Apr-02-2020, 11:55 AM
Last Post: Larz60+
  wait for element and click windows11 2 2,579 Mar-21-2020, 09:23 PM
Last Post: windows11

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020