Thursday, February 9, 2023

sample code posting snap in Instagram

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

# set up the driver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)

# navigate to the Instagram website
driver.get("https://www.instagram.com/")

# log in to your account
username_input = driver.find_element_by_name("username")
username_input.send_keys("your_username")
password_input = driver.find_element_by_name("password")
password_input.send_keys("your_password")
password_input.send_keys(Keys.RETURN)

# click the camera icon to create a new post
camera_icon = driver.find_element_by_xpath("//span[@data-icon='new-post']")
camera_icon.click()

# select the photo you want to upload
photo_input = driver.find_element_by_xpath("//input[@type='file']")
photo_input.send_keys("path_to_your_photo")

# add a caption to your post
caption_input = driver.find_element_by_xpath("//textarea[@aria-label='Write a caption…']")
caption_input.send_keys("your_caption")

# share the post
share_button = driver.find_element_by_xpath("//button[text()='Share']")
share_button.click()

# close the driver
driver.quit()

No comments:

Post a Comment