currently I'm working on a Whatsapp bot. He's working perfect, but now I wanted to expand him with threads. Unfortunately, something is not working. I watched many videos to find a solution however I didn't get it. Thank you for all your help!! Here is my code:
import threading
import time
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
def execute(amount, adressed, message):
profile_path = "user-data-dir=/home/daniel/.config/google-chrome/storing"
options = webdriver.ChromeOptions()
options.add_argument(profile_path)
browser = webdriver.Chrome(executable_path="./chromedriver", options=options)
browser.get("https://web.whatsapp.com")
time.sleep(5)
search = WebDriverWait(browser, 500).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, "#side > div.SgIJV > div > label > div > div._2_1wd.copyable-text.selectable-text")))
search.send_keys(adressed)
time.sleep(1)
search.send_keys(Keys.ENTER)
send = WebDriverWait(browser, 500).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR,
"#main > footer > div.vR1LG._3wXwX.copyable-area > div._2A8P4._2A1WX > div > div._2_1wd.copyable-text.selectable-text")))
for i in range(amount):
send.send_keys(message)
send.send_keys(Keys.ENTER)
moin = (1000, 'Hyhf', 'Hallo')
threads = []
for _ in range(2):
f1 = threading.Thread(target=execute, args=moin)
f1.start()
threads.append(f1)
for thread in threads:
thread.join()
It starts both, but just on ist affected by the code: enter image description here
Here is the error-code:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.9/threading.py", line 950, in _bootstrap_inner
self.run()
File "/usr/lib/python3.9/threading.py", line 888, in run
self._target(*self._args, **self._kwargs)
File "/home/daniel/PycharmProjects/Projekte/whatsapp/whatsapp_pyautogui/try.py", line 17, in execute
browser = webdriver.Chrome(executable_path="./chromedriver", options=options)
File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
Traceback (most recent call last):
File "/home/daniel/PycharmProjects/Projekte/whatsapp/whatsapp_pyautogui/try.py", line 43, in <module>
thread.join()
File "/usr/lib/python3.9/threading.py", line 1029, in join
self._wait_for_tstate_lock()
File "/usr/lib/python3.9/threading.py", line 1045, in _wait_for_tstate_lock
elif lock.acquire(block, timeout):
KeyboardInterrupt
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 421, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 416, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.9/http/client.py", line 1347, in getresponse
response.begin()
File "/usr/lib/python3.9/http/client.py", line 307, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.9/http/client.py", line 276, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.9/threading.py", line 950, in _bootstrap_inner
self.run()
File "/usr/lib/python3.9/threading.py", line 888, in run
self._target(*self._args, **self._kwargs)
File "/home/daniel/PycharmProjects/Projekte/whatsapp/whatsapp_pyautogui/try.py", line 31, in execute
send.send_keys(message)
File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webelement.py", line 477, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/remote_connection.py", line 374, in execute
return self._request(command_info[0], url, body=data)
File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/remote_connection.py", line 397, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "/usr/lib/python3/dist-packages/urllib3/request.py", line 79, in request
return self.request_encode_body(
File "/usr/lib/python3/dist-packages/urllib3/request.py", line 171, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "/usr/lib/python3/dist-packages/urllib3/poolmanager.py", line 330, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 719, in urlopen
retries = retries.increment(
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 400, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/usr/lib/python3/dist-packages/six.py", line 702, in reraise
raise value.with_traceback(tb)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 421, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 416, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.9/http/client.py", line 1347, in getresponse
response.begin()
File "/usr/lib/python3.9/http/client.py", line 307, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.9/http/client.py", line 276, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
Process finished with exit code 130 (interrupted by signal 2: SIGINT)