Python Forum

Full Version: Selenium big struggle
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
from selenium import webdriver
from time import sleep

class scrollDown:

    def __init__(self, username, passWord):
        self.driver = webdriver.Chrome()
        self.driver.get("https://instagram.com")
        self.username = username
        self.passWord = passWord
        sleep(2)
        self.driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input")\
            .send_keys(username)
        self.driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input")\
            .send_keys(passWord)
        self.driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button")\
            .click()
        sleep(5)

        #REMOVE NOTIFICATIONS WINDOW
        self.driver.find_element_by_xpath("/html/body/div[4]/div/div/div[3]/button[2]").click()
        sleep(3)
        print("Logged in")

        for i in range(100):
             self.driver.execute_script("window.scrollBy(0, 200)")
             sleep(1)
             print(str(i)+"%")

         #ALTERNATIVE
        headers = self.driver.find_elements_by_tag_name('header')
        for users in headers:
            pictureLocation = self.driver.find_element_by_class_name('O4GlU').get_attribute("href")
            stringLocation = str(pictureLocation)
            if "London" in stringLocation or "london" in stringLocation:
                nameLink = users.find_elements_by_tag_name('a')
                print(nameLink)


Basically im trying to find out who is in town from my instagram fee but im struggling with the element selection

Can anyone help me pls?
Your code is much more readable if you put the Python code in a code block. Can you edit it?
from selenium import webdriver
from time import sleep
 
class scrollDown:
 
    def __init__(self, username, passWord):
        self.driver = webdriver.Chrome()
        self.driver.get("https://instagram.com")
        self.username = username
        self.passWord = passWord
        sleep(2)
        self.driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input")\
            .send_keys(username)
        self.driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input")\
            .send_keys(passWord)
        self.driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button")\
            .click()
        sleep(5)
 
        #REMOVE NOTIFICATIONS WINDOW
        self.driver.find_element_by_xpath("/html/body/div[4]/div/div/div[3]/button[2]").click()
        sleep(3)
        print("Logged in")
 
        for i in range(100):
             self.driver.execute_script("window.scrollBy(0, 200)")
             sleep(1)
             print(str(i)+"%")
 
         #ALTERNATIVE
#PICK ALL THE HEADERS ==========
        headers = self.driver.find_elements_by_tag_name('header')
#LOOP THROUGH ALL OF THEM
        for users in headers:
            pictureLocation = self.driver.find_element_by_class_name('O4GlU').get_attribute("href")
            stringLocation = str(pictureLocation)
#PICK THE LOCATION ELEMENT AND CHECK IF IT MATCH "LONDON" =======
            if "London" in stringLocation or "london" in stringLocation:
                nameLink = users.find_elements_by_tag_name('a')
                print(nameLink)
I HOPE MAKES MORE SENSE NOW.