fork download
  1. from selenium import webdriver
  2. from selenium.webdriver.common.keys import Keys
  3. from time import sleep
  4.  
  5. # driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver",
  6. driver = webdriver.Chrome(
  7. # driver = webdriver.Firefox(
  8. # driver = webdriver.PhantomJS(
  9. service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
  10.  
  11. # driver.maximize_window()
  12.  
  13. driver.get("https://i...content-available-to-author-only...m.com/accounts/login")
  14. username = driver.find_element_by_name("username")
  15. password = driver.find_element_by_name("password")
  16.  
  17. username1 = 'rbn999' # change it!
  18. password1 = '12345678!@##$$%^%^&&*(*()' # change it!
  19.  
  20. username.send_keys(username1)
  21. password.send_keys(password1)
  22.  
  23. submit_button = driver.find_element_by_css_selector(
  24. '#react-root > div > article > div > div:nth-child(1) > div > form > span > button')
  25. submit_button.click()
  26.  
  27. sleep(2)
  28.  
  29. toVisit = username1
  30.  
  31. link = 'https://w...content-available-to-author-only...m.com/%s/' % (toVisit)
  32. driver.get(link)
  33.  
  34. driver.implicitly_wait(2)
  35. following = driver.find_element_by_xpath("//a[@href='/%s/following/']/span" % (toVisit))
  36. total_following = int(following.text)
  37.  
  38. print("total no. of users following: %d" % (total_following))
  39.  
  40. following.click()
  41.  
  42. def recursiveFunc(SleepSeconds):
  43. try:
  44. loaded_following = driver.find_elements_by_xpath("//ul[@class='_539vh _4j13h']/li")
  45. loaded_till_now = len(loaded_following)
  46.  
  47. while(loaded_till_now<total_following):
  48. print(loaded_following[loaded_till_now-1])
  49. print("following users loaded till now: %d" % (loaded_till_now))
  50. loaded_following[loaded_till_now-1].location_once_scrolled_into_view
  51. # driver.execute_script("arguments[0].focus();", loaded_following[loaded_till_now-1])
  52. driver.find_element_by_tag_name('body').send_keys(Keys.END) # triggers AJAX request to load more users. observed that loading 10 users at a time.
  53.  
  54. print("SleepSeconds = %d" % (SleepSeconds))
  55. sleep(SleepSeconds) # tried without sleep but throws StaleElementReferenceException. As it takes time to get the response and update the DOM
  56.  
  57. loaded_following = driver.find_elements_by_xpath("//ul[@class='_539vh _4j13h']/li")
  58. loaded_till_now = len(loaded_following)
  59. except:
  60. recursiveFunc(SleepSeconds+1)
  61.  
  62. recursiveFunc(1)
  63.  
  64. for ns in driver.find_elements_by_class_name("_6jvgy"):
  65. try:
  66. ns.find_element_by_class_name("_r4e4p").click() # unFollow button!!!
  67.  
  68. # time.sleep(2) # the same as without sleep
  69. # driver.implicitly_wait(1)
  70.  
  71. unfollow_nick = ns.find_element_by_class_name("notranslate").get_attribute("title")
  72. print(unfollow_nick) # now: prints all, but really unfollow only 15.
  73.  
  74. # c+=1
  75. # if c%4 == 0:
  76. # time.sleep(10) # seconds; unfollow 11 people instead of 15 or all; worse
  77.  
  78. except:
  79. pass
  80.  
  81. driver.quit()
  82.  
Runtime error #stdin #stdout #stderr 0.01s 9992KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
ImportError: No module named 'selenium'