from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import random

url = 'https://twitter.com/following'

driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url)

LOGIN = "salix666@mail.ru"
PASS = "pupsi4ek314"

username_locator = "#page-container > div > div.signin-wrapper > form > fieldset > div:nth-child(2) > input"
driver.find_element_by_css_selector(username_locator).send_keys(LOGIN)
password_locator = "#page-container > div > div.signin-wrapper > form > fieldset > div:nth-child(3) > input"
driver.find_element_by_css_selector(password_locator).send_keys(PASS, Keys.RETURN)
# e = driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/fieldset/div[2]/input')

tel_num = '+375447190445'

tel = '// *[ @ id = "challenge_response"]'
driver.find_element_by_xpath(tel).send_keys(tel_num, Keys.RETURN)
#following_selector = "#stream-item-user-476338341 > div > div > div.ProfileCard-userFields > div > div > a"
#e = driver.find_element_by_css_selector(following_selector).click()
# chitayu = "div > div > div.ProfileCard-actions > div > div > div > span.user-actions-follow-button.js-follow-btn.follow-button > button.EdgeButton.EdgeButton--primary.EdgeButton--small.button-text.following-text > span:nth-child(1)"
chitayu = "//div/div/div[1]/div/div/div/span[2]/button[2]"
count = 0
while count < 150:
    # e = driver.find_elements_by_css_selector(chitayu)
    e = driver.find_elements_by_xpath(chitayu)
    e = [i for i in e if i.is_displayed()]

    print len(e)
    for i in e:
        i.click()
        count += 1
        time.sleep(float(random.randint(10000, 15000))/10000)



# unretweet = "div.tweet.js-stream-tweet.js-actionable-tweet.js-profile-popup-actionable.dismissible-content.original-tweet.js-original-tweet.tweet-has-context.retweeted > div.content > div.stream-item-footer > div.ProfileTweet-actionList.js-actions > div.ProfileTweet-action.ProfileTweet-action--retweet.js-toggleState.js-toggleRt > button.ProfileTweet-actionButtonUndo.js-actionButton.js-actionRetweet.following-text"
# e = driver.find_elements_by_css_selector(unretweet)
# e = driver.find_element_by_xpath(unretweet)
#
# for i in e[:19]:
#     i.click()
#     time.sleep(float(random.randint(10000, 15000))/10000)

#coolies = [{u'domain': u'.twitter.com', u'secure': True, u'value': u'abf472f4a3bcb4cfd76e22479a5dd84d', u'expiry': 1529437783.704496, u'path': u'/', u'httpOnly': False, u'name': u'ct0'}, {u'domain': u'.twitter.com', u'secure': False, u'value': u'v1%3A152941619670488003', u'expiry': 1592488184.451045, u'path': u'/', u'httpOnly': False, u'name': u'guest_id'}, {u'domain': u'twitter.com', u'name': u'lang', u'value': u'ru', u'path': u'/', u'httpOnly': False, u'secure': False}, {u'domain': u'.twitter.com', u'secure': False, u'value': u'"v1_5jVdNIuOjm9ZO1QrV6eORA=="', u'expiry': 1592488184.450999, u'path': u'/', u'httpOnly': False, u'name': u'personalization_id'}, {u'domain': u'.twitter.com', u'secure': True, u'value': u'"u=383335682"', u'expiry': 1844776192.500618, u'path': u'/', u'httpOnly': False, u'name': u'twid'}, {u'domain': u'.twitter.com', u'secure': False, u'value': u'GA1.2.1973448638.1529416189', u'expiry': 1529502595, u'path': u'/', u'httpOnly': False, u'name': u'_gid'}, {u'domain': u'.twitter.com', u'secure': False, u'value': u'GA1.2.1387155670.1529416189', u'expiry': 1592488195, u'path': u'/', u'httpOnly': False, u'name': u'_ga'}, {u'domain': u'.twitter.com', u'secure': False, u'value': u'"HBERAAA="', u'expiry': 1844776192.500327, u'path': u'/', u'httpOnly': False, u'name': u'ads_prefs'}, {u'domain': u'.twitter.com', u'secure': True, u'value': u'4ReWgwFoZz2Kv092u7vxDUM1OjA6UZUOffb3SrSE', u'expiry': 1576676992.5004, u'path': u'/', u'httpOnly': True, u'name': u'kdt'}, {u'domain': u'.twitter.com', u'secure': False, u'value': u'1', u'expiry': 1844776192.500469, u'path': u'/', u'httpOnly': False, u'name': u'remember_checked_on'}, {u'domain': u'.twitter.com', u'name': u'_twitter_sess', u'value': u'BAh7DCIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCGMiTxhkAToHaWQiJTQ5%250AZDVhZTEwMDM5YjFhMzYwM2FjNDg1OWYxZjViMDFhOgxjc3JmX2lkIiU0YjQx%250ANDY3MzRlNzcwYzI5NDk0NDRjMGMwMmQ0MjcwYjofbG9naW5fdmVyaWZpY2F0%250AaW9uX3VzZXJfaWRpBAI92RY6ImxvZ2luX3ZlcmlmaWNhdGlvbl9yZXF1ZXN0%250AX2lkIiU2aVBYaGtWQWxaZktteURUa1k2aE16eldSWGZNY0NhNToJdXNlcmkE%250AAj3ZFg%253D%253D--c557a83f88438f871b368a12705946bd2898da4d', u'path': u'/', u'httpOnly': True, u'secure': True}, {u'domain': u'.twitter.com', u'secure': True, u'value': u'8804b166eaad2d865c19f400f9c9c41c0981db92', u'expiry': 1844776192.50068, u'path': u'/', u'httpOnly': True, u'name': u'auth_token'}, {u'domain': u'.twitter.com', u'secure': True, u'value': u'1', u'expiry': 1560865793.096713, u'path': u'/', u'httpOnly': True, u'name': u'csrf_same_site_set'}, {u'domain': u'.twitter.com', u'secure': True, u'value': u'1', u'expiry': 1560952193.096874, u'path': u'/', u'httpOnly': True, u'name': u'csrf_same_site'}]
#for i in cookies:
#    driver.add_cookie(i)
