Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from selenium import webdriver
- 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
- from selenium.common.exceptions import NoSuchElementException
- from selenium.common.exceptions import ElementClickInterceptedException
- from selenium.webdriver.common.action_chains import ActionChains
- from time import sleep
- driver = webdriver.Firefox()
- wait = WebDriverWait(driver, 50)
- actions = ActionChains(driver)
- driver.get('https://twitter.com')
- scroll_path = "/html/body/div[1]/div/div/div[2]/main/div/div/div/div[1]/div/div[3]/section/div/div"
- after_img_path = "div/div/article/div/div/div[2]/div[1]/div/div/div/div/div[2]/div/div[2]/div/a/div[3]/div/div[2]/div/img"
- username_path = "div/div/article/div/div/div[2]/div[2]/div[1]/div/div[1]/div/div/div[2]/div/div[1]/a"
- clearname_path = "div/div/article/div/div/div[2]/div[2]/div[1]/div/div[1]/div/div/div[1]/div/a/div/div[1]/span/span"
- post_id_path = "div/div/article/div/div/div[2]/div[2]/div[1]/div/div[1]/div/div/div[2]/div/div[3]/a"
- timestamp_path = "div/div/article/div/div/div[2]/div[2]/div[1]/div/div[1]/div/div/div[2]/div/div[3]/a/time"
- wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div/div/div/div[2]/main/div/div/div[1]/div[1]/div/div[3]/div[4]/a/div'))).click() # login
- wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div/div/div/div[1]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div/div[4]/label/div'))).click() # username
- body = driver.find_element(By.TAG_NAME, "body") # send keystrokes to body
- body.send_keys("DiegoCab3110")
- body.send_keys(Keys.ENTER)
- wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/div/div/div/div[1]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[3]/div/label/div/div[2]/div[1]/input"))).send_keys("slippy66") # password
- driver.find_element(By.XPATH, '/html/body/div/div/div/div[1]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[2]/div/div[1]/div/div/button').click() # log in
- wait.until(EC.url_to_be("https://x.com/home"))
- def navigate(tag):
- driver.get(f"https://x.com/{tag}")
- def add_to_list(categories: list):
- try:
- wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/div/div[2]/main/div/div/div/div/div/div[3]/div/div/div/div/div[1]/div[2]/button[1]/div'))).click()
- sleep(3)
- wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/div/div[1]/div[2]/div/div/div/div[2]/div/div[3]/div/div/div/a[2]'))).click()
- container = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'section.css-175oi2r:nth-child(2) > div:nth-child(2) > div:nth-child(1)')))
- sleep(2)
- container.click()
- contained = container.find_elements(By.XPATH, './div')
- selectors = [
- "div[role='dialog'] div[data-testid='listCell']",
- "div[role='dialog'] div[dir='ltr']",
- "//div[@role='dialog']//div[@data-testid='listCell']"
- ]
- print('\t' + str(categories))
- found = False
- categories_copy = categories.copy()
- for selector in selectors:
- try:
- elements = driver.find_elements(By.CSS_SELECTOR if not selector.startswith("//") else By.XPATH, selector)
- for element in elements:
- try:
- text = element.text.strip()
- if text.lower() in categories_copy:
- try:
- print(f"\tFound matching list: {text}")
- wait.until(EC.element_to_be_clickable(element))
- # Enhanced obstruction handling
- max_attempts = 10
- for attempt in range(max_attempts):
- try:
- element.click()
- break
- except ElementClickInterceptedException:
- print(f'\tintercepted. removing obstacles (attempt {attempt + 1})...')
- # Get all elements that might be obstructing
- script = """
- function getObstructingElements(element) {
- var rect = element.getBoundingClientRect();
- return document.elementsFromPoint(rect.left + rect.width/2, rect.top + rect.height/2)
- .filter(e => e !== element && e !== document.documentElement && e !== document.body);
- }
- var targetElement = arguments[0];
- var obstructing = getObstructingElements(targetElement);
- obstructing.forEach(function(element) {
- if (element.parentNode) {
- element.parentNode.removeChild(element);
- }
- });
- """
- driver.execute_script(script, element)
- sleep(1)
- categories_copy.remove(text.lower())
- if len(categories_copy) == 0:
- found = True
- break
- except Exception as e:
- print(f"\tError clicking element: {str(e)}")
- continue
- except Exception as e:
- print(f"Error checking element: {str(e)}")
- continue
- if found:
- break
- except Exception as e:
- print(f"Error with selector {selector}: {str(e)}")
- continue
- if not found:
- print(f"Could not find all lists: {categories_copy}")
- return
- driver.find_element(By.XPATH, '/html/body/div[1]/div/div/div[1]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div/div/div/div/div/div/div/div[3]/button').click()
- except Exception as e:
- print(f"Error in add_to_list: {str(e)}")
- def unfollow():
- follow_button = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/div/div[2]/main/div/div/div/div/div/div[3]/div/div/div/div/div[1]/div[2]/div[2]/div[1]/button/div/span/span')))
- # /html/body/div[1]/div/div/div[2]/main/div/div/div/div[1]/div/div[3]/div/div/div/div/div[1]/div[2]/div/div[1]/button/div/span/span
- if follow_button.text == "Following":
- #add_to_list('diegos list')
- follow_button.click()
- confirm_button = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/div/div[1]/div[2]/div/div/div/div/div/div[2]/div[2]/div[2]/button[1]/div/span/span'))).click()
- if __name__ == '__main__':
- with open('to_use.json', 'r') as file:
- data = json.load(file)
- print(len(data))
- unfollow_flag = False
- c = 0
- for key, values in data.items():
- c += 1
- print(str(c).zfill(2) + ': ' + key)
- navigate(key)
- categories = []
- for value in values:
- if value != 'unfollow':
- categories.append(value)
- else:
- unfollow_flag = True
- if categories:
- add_to_list(categories)
- if unfollow_flag:
- sleep(2)
- unfollow()
- unfollow_flag = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement