0

Hi I am using Firefox on Mac. I try to use selenium on python to click the like button for the first 3 picture in the tag. but, the code keep have error in checking whether the like button is click or not, and the like button is obstruct by other item.

Error I have

  1. ElementClickInterceptedException: Element is not clickable at point (311,460) because another element obscures it
  2. AttributeError: 'WebDriver' object has no attribute 'findElement'

Below is the code.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
import time

sleeptime = 10

#login function for account etc
def login(browser):
    browser.maximize_window()
    browser.get("https://www.instagram.com")
    
    time.sleep(sleeptime)
    
    username = browser.find_element(By.NAME,"username")
    password = browser.find_element(By.NAME,"password")
    
    #grab all the button (take care if button change position, currently login is at the first) [0] is the first button
    login = browser.find_element(locate_with(By.TAG_NAME, "button").below(password))
    
    #input the username and password
    username.send_keys("Hkk_james38")
    password.send_keys("myfmox-tidgab-xotNo0")
    login.click()
    
    
    
    #to hold the browser to prevent it close itselfs
    time.sleep(sleeptime)

def Visit_Tag(driver, url):
    sleeptime = 6
    driver.get(url)
    time.sleep(sleeptime)
    
    #select all the pictures and save in the list
    pictures = driver.find_elements_by_css_selector("div[class = '_9AhH0']")
    print(pictures)
    image_count = 0 
    
    #click the like in each picture by loop
    for picture in pictures:
        
        if image_count > 3:
            break
                
        picture.click()
        time.sleep(sleeptime)
        
        #click the like button, cehck if the like button is click or not
        if driver.find_elements(By.XPATH,"//article//section//button//*[@aria-label='Unlike']").size() > 0:
            like = driver.find_element(By.XPATH,"//article//section//button//*[@aria-label='Like']")
        like.click()
       
        #close the tag
        close = driver.find_element(By.XPATH,"//article//section//button//*[@aria-label='Close']")
        close.click()
        
        image_count += 1
        
    
    
#main function to execute
def main():
    #find webdriver location for firefox
    service = Service('/usr/local/bin/geckodriver')
    browser = webdriver.Firefox(service = service)
    login(browser)
    
    #tag for the pages u will follow and interested in
    tags = {
        "programming",
        
        }
    
    websites = []
    
    for tag in tags:
        Visit_Tag(browser, f"https://www.instagram.com/explore/tags/{tag}")
        
    
main()
Harry Hau
  • 39
  • 5

1 Answers1

0

As clearly mentioned by the error message you are seeing there is no such method or attribute findElements existing for the WebDriver object.
Instead of

if driver.findElements(By.XPATH("//article//section//button//*[@aria-label='Unlike']")).size() > 0:

Try using

if driver.find_elements(By.XPATH("//article//section//button//*[@aria-label='Unlike']")).size() > 0:

As about the

ElementClickInterceptedException: Element is not clickable at point (311,460) because another element obscures it

error: it's unclear what code line cases that error.
You did not provide the whole error traceback nor the web page URL so we can't guess that.

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Sorry. I have updated my full code. – Harry Hau Nov 04 '21 at 00:36
  • Your full code contains login and password. – Denis Romaniuk Nov 04 '21 at 00:44
  • Have you seen this topic? https://stackoverflow.com/a/48667924/7427923 – Denis Romaniuk Nov 04 '21 at 00:48
  • @HarryHau Thanks for pasting the code. But it is still unclear what code line is causing the `ElementClickInterceptedException` – Prophet Nov 04 '21 at 07:53
  • @DenisRomaniuk I saw the topic. This definitely does NOT provide enough information what code line causes the `ElementClickInterceptedException`. It is not clear even now. As about the credentials - they surely of some test account, not the real account of OP. – Prophet Nov 04 '21 at 07:56
  • If it would an error of python module, you would get python exception (with detailed traceback). Otherwise, it may be an exception, in selenium geckodriver (read as agent, which your module connects to, or runs in subprocess). And such errors can appear in your stdin directly from subprocess. At least it explains why you dont get an output with line number, where an exception occured. You would can add `logging` module and print additional debugging messages when "click" on page elements. – Denis Romaniuk Nov 04 '21 at 08:20