Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #151, bump python version, mimic human typing behavior #154

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions alright/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Expand All @@ -37,7 +38,7 @@ def __init__(self, browser=None, time_out=600):

if not browser:
browser = webdriver.Chrome(
ChromeDriverManager().install(),
service=ChromeService(ChromeDriverManager().install()),
options=self.chrome_options,
)

Expand Down Expand Up @@ -139,11 +140,21 @@ def find_user(self, mobile) -> None:
self.mobile = mobile
link = self.get_phone_link(mobile)
self.browser.get(link)
time.sleep(3)
# check if user profile exists or not
nr_xpath = '//*[@id="main"]/header/div[2]/div/div/div/span'
nr_not_found_xpath = '//*[@id="app"]/div/span[2]/div/span/div/div/div/div/div/div[2]/div/button'

ctrl_element = self.wait.until(
lambda ctrl_self: ctrl_self.find_elements(By.XPATH, nr_not_found_xpath)
or ctrl_self.find_elements(By.XPATH, nr_xpath)
)[0]
if ctrl_element.tag_name == 'span': # user does exists and the span will have the number of the user
return True
except UnexpectedAlertPresentException as bug:
LOGGER.exception(f"An exception occurred: {bug}")
time.sleep(1)
self.find_user(mobile)
return False

def find_by_username(self, username):
"""find_user_by_name ()
Expand Down Expand Up @@ -467,17 +478,24 @@ def send_message(self, message, timeout=0.0):
EC.presence_of_element_located((By.XPATH, inp_xpath))
)
for line in message.split("\n"):
input_box.send_keys(line)
# mimick human like typing behaviour
for word in line.split():
input_box.send_keys(word)
input_box.send_keys(Keys.SPACE)
time.sleep(1)

ActionChains(self.browser).key_down(Keys.SHIFT).key_down(
Keys.ENTER
).key_up(Keys.ENTER).key_up(Keys.SHIFT).perform()
if timeout:
time.sleep(timeout)
input_box.send_keys(Keys.ENTER)
LOGGER.info(f"Message sent successfuly to {self.mobile}")
return True
except (NoSuchElementException, Exception) as bug:
LOGGER.exception(f"Failed to send a message to {self.mobile} - {bug}")
LOGGER.info("send_message() finished running!")
return False

def send_direct_message(self, mobile: str, message: str, saved: bool = True):
if saved:
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
platformdirs
selenium
webdriver-manager
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"webdriver-manager",
],
include_package_data=False,
python_requires=">=3.6",
python_requires=">=3.7",
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
Expand Down