· 6 years ago · Jul 03, 2019, 12:48 PM
1Traceback (most recent call last):
2 File "main.py", line 144, in <module>
3 bot.openPost(postLink[j])
4 File "main.py", line 65, in openPost
5 self.browser.get(postLink)
6 File "C:\Users\Asus\PycharmProjects\Twitter\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 332, in get
7 self.execute(Command.GET, {'url': url})
8 File "C:\Users\Asus\PycharmProjects\Twitter\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
9 self.error_handler.check_response(response)
10 File "C:\Users\Asus\PycharmProjects\Twitter\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
11 raise exception_class(message, screen, stacktrace)
12selenium.common.exceptions.WebDriverException: Message: no such session
13 (Driver info: chromedriver=2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9),platform=Windows NT 10.0.17134 x86_64)
14
15
16During handling of the above exception, another exception occurred:
17
18Traceback (most recent call last):
19 File "main.py", line 153, in <module>
20 bot.exitFromBrowser()
21 File "main.py", line 62, in exitFromBrowser
22 self.closeBrowser()
23 File "main.py", line 56, in closeBrowser
24 self.browser.close()
25 File "C:\Users\Asus\PycharmProjects\Twitter\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 687, in close
26 self.execute(Command.CLOSE)
27 File "C:\Users\Asus\PycharmProjects\Twitter\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
28 self.error_handler.check_response(response)
29 File "C:\Users\Asus\PycharmProjects\Twitter\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
30 raise exception_class(message, screen, stacktrace)
31selenium.common.exceptions.WebDriverException: Message: no such session
32 (Driver info: chromedriver=2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9),platform=Windows NT 10.0.17134 x86_64)
33
34
35
36import json
37import time
38
39from selenium import webdriver
40from selenium.common.exceptions import TimeoutException
41
42login = ""
43password = ""
44secretKey = ""
45accounts = []
46
47twitterLink = "https://twitter.com/login"
48
49
50class Twitter:
51 def __init__(self, email, password, secretKey):
52 self.browserProfile = webdriver.ChromeOptions()
53 self.browserProfile.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
54 self.browser = webdriver.Chrome('chromedriver.exe', chrome_options=self.browserProfile)
55 self.email = email
56 self.password = password
57 self.secretKey = secretKey
58 self.browser.delete_all_cookies()
59
60 def signIn(self):
61 self.browser.get(twitterLink)
62
63 try:
64 emailInput = self.browser.find_element_by_class_name("js-username-field")
65 passwordInput = self.browser.find_element_by_class_name("js-password-field")
66
67 emailInput.send_keys(self.email)
68 self.browser.implicitly_wait(1)
69
70 passwordInput.send_keys(self.password)
71 self.browser.implicitly_wait(1)
72
73 self.browser.find_element_by_class_name("EdgeButtom--medium").click()
74 time.sleep(3)
75
76 try:
77 secretKey = self.browser.find_element_by_class_name("Edge-textbox")
78
79 secretKey.send_keys(self.secretKey)
80 self.browser.implicitly_wait(1)
81
82 self.browser.find_element_by_class_name("EdgeButton--primary").click()
83 time.sleep(3)
84 except Exception:
85 self.exitFromBrowser()
86
87 except TimeoutException:
88 print("Loading took too much time!")
89
90 def closeBrowser(self):
91 self.browser.close()
92
93 def __exit__(self, exc_type, exc_value, traceback):
94 self.closeBrowser()
95
96 def exitFromBrowser(self):
97 self.closeBrowser()
98
99 def openPost(self, postLink):
100 self.browser.get(postLink)
101
102 def likePost(self):
103 self.browser.find_element_by_class_name("js-actionFavorite").click()
104 time.sleep(1)
105
106 def retweetPost(self):
107 self.browser.find_element_by_class_name("js-actionRetweet").click()
108 time.sleep(1)
109 self.browser.find_element_by_class_name("retweet-action").click()
110 time.sleep(1)
111
112
113def readTxtFileWithPostLink():
114 filepath = "postLink.txt"
115 my_list = []
116
117 with open(filepath) as f:
118 lines = f.readlines() # list containing lines of file
119 columns = [] # To store column names
120
121 i = 1
122 for line in lines:
123 line = line.strip() # remove leading/trailing white spaces
124 my_list.append(line)
125
126 # pretty printing list of dictionaries
127 list1 = json.dumps(my_list)
128 postlinks = json.loads(list1)
129 return postlinks
130
131
132class FileRead:
133 def readTxtFileWithAccounts(self):
134 filepath = "accounts.txt"
135 my_list = []
136
137 with open(filepath) as f:
138 lines = f.readlines() # list containing lines of file
139 columns = [] # To store column names
140
141 i = 1
142 for line in lines:
143 line = line.strip() # remove leading/trailing white spaces
144 if line:
145 if i == 1:
146 columns = [item.strip() for item in line.split(':')]
147 i = i + 1
148 else:
149 d = {} # dictionary to store file data (each line)
150 data = [item.strip() for item in line.split(':')]
151 for index, elem in enumerate(data):
152 d[columns[index]] = data[index]
153
154 my_list.append(d) # append dictionary to list
155
156 # pretty printing list of dictionaries
157 list1 = json.dumps(my_list)
158 account = json.loads(list1)
159 return account
160
161
162if __name__ == "__main__":
163 print("Start Twitter Bot")
164
165 like = input("Do you want like post? (yes/no): ")
166 retweet = input("Do you want retweet post? (yes/no): ")
167
168 file = FileRead()
169 accounts = file.readTxtFileWithAccounts()
170 postLink = readTxtFileWithPostLink()
171
172 j = 0
173 while j < len(postLink):
174 i = 0
175 while i < len(accounts):
176 try:
177 bot = Twitter(accounts[i]['login'], accounts[i]['pass'], accounts[i]['secretKey'])
178 bot.signIn()
179 bot.openPost(postLink[j])
180
181 if like == "yes":
182 bot.likePost()
183 if retweet == "yes":
184 bot.retweetPost()
185
186 bot.exitFromBrowser()
187 except Exception:
188 bot.exitFromBrowser()
189 i += 1
190 j += 1