0

So I'm trying to implement a program using selenium that upvotes the top post in my mostly private subreddit. So far, I've been able to implement the logging in and clicking on the upvote button part but the votes aren't actually registering on Reddit as far as I can see. I'm fairly new to python and have learned most of the basics so I'm doing this just for the sake of experimentation and curiosity. Here's my code I would really appreciate it if you could maybe try to help me out.

from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
import os
import random

webs = input("Complete Post URL: ")
username = os.getenv("USERNAME")
userProfile = "C:\\Users\\" + username + "\\AppData\\Local\\Google\\Chrome\\User Data\\Default"
chrome_options = Options()

chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--disable-extensions")

chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-javascript")
chrome_options.add_argument("--disable-rtc-smoothness-algorithm")
chrome_options.add_argument("--disable-webrtc-encryption")
chrome_options.add_argument("--disable-webrtc-hw-decoding")
chrome_options.add_argument("--incognito")
chrome_options.add_argument(
    '--user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
    'Chrome/86.0.4240.75 Safari/537.36"')
chrome_options.add_experimental_option("excludeSwitches",
                                       ["ignore-certificate-errors", "safebrowsing-disable-download-protection",
                                        "safebrowsing-disable-auto-update", "disable-client-side-phishing-detection",
                                        "enable-automation"])

x = str(random.randint(700, 1920))
y = str(random.randint(700, 1080))
chrome_options.add_argument(f"--window-size={x,y}")
driver = webdriver.Chrome('chromedriver.exe', options=chrome_options)

driver.get("https://old.reddit.com")
user = driver.find_element_by_xpath('//*[@id="login_login-main"]/input[2]')
user.send_keys(useritem)
pwd = driver.find_element_by_xpath('//*[@id="login_login-main"]/input[3]')
pwd.send_keys(passitem)
login = driver.find_element_by_xpath('//*[@id="login_login-main"]/div[4]/button')
login.click()
time.sleep(3.45)
driver.get(webs)
xp = webs.split('/')
xp = xp[-3]

try:
    time.sleep(5)
    upvote = driver.find_element_by_xpath(f'//*[@id="upvote-button-t3_{xp}"]/span/i')
    upvote.click()
    print(useritem + " Upvoted!")
    time.sleep(15)
    driver.close()
except TimeoutException as t:
    print(t.msg)
Nanogines
  • 11
  • 1
  • 5
  • What's the error? - does anything happen? Have you tried debugging it to see what happens? - I can see you're running a lot of tags, are you sure you want all of them... namely, to disable javascript? (That could be how the vote is submitted to the server) – RichEdwards Oct 12 '20 at 09:43
  • @RichEdwards There's no error it just doesn't update the post score. It upvotes as a normal person would do but it doesn't seem to function that way – Nanogines Oct 12 '20 at 09:46
  • Try removing this line `chrome_options.add_argument("--disable-javascript")` and running again. – Naveen Oct 12 '20 at 10:11
  • @Naveen done that doesn't seem to impact most of anything – Nanogines Oct 12 '20 at 11:34
  • It is possible this is an xpath issue. Can you try with upwork xpath like `(//div[@data-author]//*[@data-event-action="upvote"])[1]` – Naveen Oct 12 '20 at 13:16
  • Could be a bot detection method implemented by Reddit. They might be able to sense you are using Selenium and are therefore blocking your votes. This is actually very common and the only way to try to remove detection is by removing the `$cdc` line. Check more here: https://stackoverflow.com/a/51236874/12514570 – Rohan Shah Oct 13 '20 at 15:57

0 Answers0