Hello, I want to start by saying thank you to anyone who takes the time just to read my question. Basically what my problem is… I’m building an Instagram bot. After logging in and navigating to a users page I need to click the followers list. In order to click the button i’m using self.driver.find_element_by_xpath("//*[text()='1,761']").click()
. In order for this function to run I need the number of followers, because that number will change I am running this to find that value and format it with a comma.
username = "lompocvalleychamber"
url = 'https://www.instagram.com/' + username
r = requests.get(url).text
start = '"edge_followed_by":{"count":'
end = '},"followed_by_viewer"'
followers= r[r.find(start)+len(start):r.rfind(end)]
followers = followers
print(format(int(followers),","))
Now what I need is for that number to be printed in the xpath value so that the function can properly run. Any help will be greatly appreciated. Here is the full code.
from selenium import webdriver
import os
import time
import requests
username = "lompocvalleychamber"
url = 'https://www.instagram.com/' + username
r = requests.get(url).text
start = '"edge_followed_by":{"count":'
end = '},"followed_by_viewer"'
followers= r[r.find(start)+len(start):r.rfind(end)]
followers = followers
print(format(int(followers),","))
class InstagramBot:
def __init__(self, username, password, follower):
"""
Initializes an instance of the instagram bot class.
call the login method to athenticate user a user with IG.
Args:
username:str: the Instagram usernasme for a user
password:str: the Instagram password for a user
driver.selenuim.webdriver.Chrome: The chromedriver is used to automate browser actions
Attributes:
"""
self.follower = follower
self.username = username
self.password = password
self.driver = webdriver.Chrome(executable_path='/Users/tylergomez/Desktop/FolderForPython.py/chromedriver')
self.login()
def login(self):
self.driver.get('https://www.instagram.com/')
time.sleep(2)
self.driver.find_element_by_name('username').send_keys(self.username)
self.driver.find_element_by_name('password').send_keys(self.password)
time.sleep(2)
self.driver.find_elements_by_xpath("//div[contains(text(),'Log In')]")[0].click()
time.sleep(2)
self.driver.find_element_by_xpath("/html/body/div[1]/section/nav/div[2]/div/div/div[2]/input").send_keys(self.follower)
time.sleep(5)
self.driver.find_element_by_xpath("/html/body/div[1]/section/nav/div[2]/div/div/div[2]/div[3]/div[2]/div/a[1]").click()
time.sleep(30)
self.driver.find_element_by_xpath("//*[text()='followers']").click()
time.sleep(15)
self.driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]/ul/div/li[1]/div/div[3]/button").click()
if __name__ == '__main__':
ig_bot = InstagramBot('bar_harper','tyler80520','lompoc valley chamber')