Skip to content

Instantly share code, notes, and snippets.

@rena2019
Last active November 13, 2024 18:26
Show Gist options
  • Save rena2019/620012d8adb1d7057d08180c22da9449 to your computer and use it in GitHub Desktop.
Save rena2019/620012d8adb1d7057d08180c22da9449 to your computer and use it in GitHub Desktop.
Post @ Threads with Selenium
def threads_post(account="", pwd="", post="", display=False):
#2024-02-18 threads post (with selenium)
# TODO replace sleep with wait
# try:
# element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "tag_id"))
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
start = time.time()
chrome_options = Options()
if display:
#zum testen mit Fenster
chrome_options.add_argument("--window-size=1920x1080")
service = Service(executable_path=r"D:\tools\chromedriver-win64\chromedriver.exe")
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.implicitly_wait(10) # seconds
driver.get(f"https://www.threads.net/")
# Allow the use of cookies from Threads by Instagram on this browser?
elem = driver.find_element(By.XPATH, '//div[text()="Decline optional cookies"]')
elem.click()
#Continue with Instagram
elem = driver.find_element(By.XPATH, '//*[contains(text(), "Continue with Instagram")]')
elem.click()
#enter username
#elem = driver.find_element(By.XPATH, '//form//input[1]')#input[@type='text']')
#elem = driver.find_elements(By.XPATH, "//div[contains(@class, 'x1a6qonq x6ikm8r x10wlt62 xj0a0fe x126k92a x6prxxf x7r5mf7')]")
#elem = driver.find_element(By.CSS_SELECTOR, "[class='x1i10hfl x9f619 xggy1nq x1s07b3s x1kdt53j x1a2a7pz x90nhty x1v8p93f xogb00i x16stqrj x1ftr3km xyi19xy x1ccrb07 xtf3nb5 x1pc53ja x13fuv20 xu3j5b3 x1q0q8m5 x26u7qi x178xt8z xm81vs4 xso031l xy80clv xp07o12 xjohtrz x1a6qonq xyamay9 x1pi30zi x1l90r2v x1swvt13 x1yc453h xh8yej3 x1e899rk x1sbm3cl x1rpcs5s x1c5lum3 xd5rq6m']")
elem = driver.find_element(By.XPATH, "//input[@placeholder='Username, phone number or email address']")
elem.send_keys(account)
#password
elem = driver.find_element(By.XPATH, "//input[@placeholder='Password']")
elem.send_keys(pwd)
elem.send_keys(Keys.ENTER)
#post
#1. <click> to open dialog
elem = driver.find_element(By.XPATH, '//span[contains(text(), "Start a thread...")]')
elem.click()
#2. new thread dialog
elem = driver.find_element(By.XPATH, '//p[@class="xdj266r x11i5rnm xat24cr x1mh8g0r"]')
elem.click()
elem.send_keys(post)
#3. post button
elem = driver.find_elements(By.XPATH, '//div[@class="xc26acl x6s0dn4 xtlf3c x78zum5 xl56j7k x6ikm8r x10wlt62 x1swvt13 x1pi30zi xlyipyv"]')
elem[1].click()
#todo use wait
time.sleep(5)
driver.close()
end = time.time()
print(end - start)
if __name__ == "__main__":
threads_post(account="<YOUR ACCOUNT>", pwd="<PASSWORD>", post="TEST MIT SELENIUM", display=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment