0

I want to write a Python file that contains functions which we want to use in our project. We are working on a Selenium web scraping bot fot Instagram. Right now we write all the functions in the scripts but we want to make a "function" file which we will import and use for our scripts. But the thing is that VS code does not use autocompletion when I want to use a webdrivers function like driver.find_element_by_xpath(cookies_button_xpath).click(). The function file (not finished yet) looks like this:

import time

from selenium.webdriver.common.keys import Keys
from selenium import webdriver

# set constants for functions to run
WEBSITE_PRE_FIX = 'https://www.instagram.com/'
FORBIDDEN_CAPTION_WORDS = ['link in bio','buy now','limited time']

def open_ig(driver: webdriver):
    # opens the website and waits till it is loaded
    driver.get(WEBSITE_PRE_FIX)
    time.sleep(2)

    # accept cookies
    cookies_button_xpath = "/html/body/div[4]/div/div/button[1]"
    driver.find_element_by_xpath(cookies_button_xpath).click()

def login(driver: webdriver, username, password):
    time.sleep(2)
    
    # fill in user name and password and log in
    username_box_xpath = '/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input'
    username_element = driver.find_element_by_xpath(username_box_xpath)
    username_element.send_keys(username)

    password_box_xpath = '/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input'
    password_element = driver.find_element_by_xpath(password_box_xpath)
    password_element.send_keys(password)
    password_element.send_keys(Keys.ENTER)

    # click on do not save username and password + do not turn on notifications
    time.sleep(3)
    dont_save_username_button_password_xpath = '/html/body/div[1]/section/main/div/div/div/div/button'
    dont_save_username_button_element = driver.find_element_by_xpath(dont_save_username_button_password_xpath)
    dont_save_username_button_element.click()

So the code does work (as in it runs and does what I want) but I would like to know if we can write the function file another way so things like autocompletion en the color filters work. I'm not completely sure if it is possible. If there is any other way to write the functions file, all recommendations are welcome.

1 Answer 1

1

Have you tried writing the functions file as a simple class?

class FunctionsFile():
    def __init__(self):
        self.website_pre_fix = 'https://www.instagram.com/'
        self.forbidden_capture_words = ['link in bio','buy now','limited time']

    def open_ig(self, driver: webdriver):
        # opens the website and waits till it is loaded
        driver.get(WEBSITE_PRE_FIX)
        time.sleep(2)

        # accept cookies
        cookies_button_xpath = "/html/body/div[4]/div/div/button[1]"
        driver.find_element_by_xpath(cookies_button_xpath).click()

    def login(self, driver: webdriver, username, password):
        time.sleep(2)
    
        # fill in user name and password and log in
        username_box_xpath = '/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input'
        username_element = driver.find_element_by_xpath(username_box_xpath)
        username_element.send_keys(username)

        password_box_xpath = '/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input'
        password_element = driver.find_element_by_xpath(password_box_xpath)
        password_element.send_keys(password)
        password_element.send_keys(Keys.ENTER)

        # click on do not save username and password + do not turn on notifications
        time.sleep(3)
        dont_save_username_button_password_xpath = '/html/body/div[1]/section/main/div/div/div/div/button'
        dont_save_username_button_element = driver.find_element_by_xpath(dont_save_username_button_password_xpath)
        dont_save_username_button_element.click()

You can then instantiate the class in any file. If in same directory:

from FunctionsFile import FunctionsFile

funcs = FunctionsFile()
funcs.open_ig(driver)

That should use the standard VS Code color schemes and autocompletion. (I think anyway).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your suggestion. I've tried it but the webdriver functions still do not show up. For some reason VS Code does not know that 'driver' is a webdriver selenium object. Which is pretty understandable because I declare it in the other file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.