0

In before someone comments on how horrible code looks (Unoptimized code etc.), please note that I just got myself into Python, and have less than a day worth of experience. Obviously I will tidy up code after that.

So, what I have here is essentially script for automated comment downvoting, nothing impressive or creative, but it was first thing that I came up in my mind.

Problem I am facing here, is that, after the Main Loop is finished, I get error about downvote_button element, not being clickable. Which basically means, It could be anything starting from random banners popping up, up till browser screen window not being maximized. (As far as I learned)

Thing is, I actually tested each of these scripts separately, and both of them work without a single problem, but as soon as I try to run them in order that I displayed in my code, I get this error.

I understand that the problem probably has something to do with the way I made 2 loops, instead of 1 loop inside another, but since I am really bad at making loops correctly, this is the best I could come up with.

Would love if anyone could point out to me, what's wrong with my code, so that I could continue working on this project :)

Thanks!

#--------------------------------- Add required modules -----------------------------------
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

#---------------------------------- Load Default chrome user settings ---------------------
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\PC\\AppData\\Local\\Google\\Chrome\\User Data\\Default")

driver = webdriver.Chrome(executable_path=r'C:/Projects/htdocs/python/chromedriver.exe', chrome_options=options) 

#-------------------------------------- Driver setup --------------------------------------
driver.get('https://website.with.comment.section.com') #Target website
driver.implicitly_wait(2)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") #Scroll website windows to the bottom of the page to display "Load comments button"
driver.implicitly_wait(1)

load_button = driver.find_element_by_class_name('comments__load-page')
downvote_button = driver.find_elements_by_tag_name('[data-action = "down"]')

#-------------------------------------- Main Loop ------------------------------------------
# Continuously press load_button, while load_button is visible

While load_button.is_displayed():
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Scroll for new comments to appear.
    driver.implicitly_wait(1)
    load_button.click()
    time.sleep(2) #Wait for comments and button to load, before next increment (In case of lag/slow internet)

#--------------------------------------- Secondary Loop -------------------------------------  
# As soon as Main Loop is finished, start Secondary Loop, to press downvote button for each and every comment which has downvote_button.

For button in downvote_button:
    button.click()

Message: element click intercepted: Element <div data-action="down" class="comment__rating-action comment__rating-action_active">...</div> is not clickable at point (829, 11). Other element would receive the click: <div class="header-menu">...</div>

PS. All comments are being loaded dynamically, so there are no redirects or any pop-ups that could be a reason for script to fail.

Vairis
  • 227
  • 3
  • 13
  • Would you want to declare `downvote_button` after the `While` loop? Does that capture more "down" buttons after you load them? – BruceWayne Aug 03 '20 at 18:46
  • Yes, after the While loop, script would allow browser to display additional 100+ comments, and essentially allow me to downvote every single comment, instead of first 20 or so. – Vairis Aug 03 '20 at 18:54
  • Well, turns out for script to "see" html element it needs to be "visible" within viewport. That means that I had to add driver.execute_script("window.scroll(0, 0);") after Main Loop. Hope this helps someone. – Vairis Aug 03 '20 at 21:42

0 Answers0