Skip to content

Commit

Permalink
Merge pull request #19 from hhslepicka/many_fixes
Browse files Browse the repository at this point in the history
Many fixes from Testing
  • Loading branch information
hhslepicka committed Jan 13, 2022
2 parents 687047c + 999d571 commit a1245ce
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion botcity/web/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .bot import WebBot, Browser, BROWSER_CONFIGS # noqa: F401, F403
from .bot import WebBot, Browser, BROWSER_CONFIGS, By # noqa: F401, F403

from botcity.web._version import get_versions
__version__ = get_versions()['version']
Expand Down
33 changes: 27 additions & 6 deletions botcity/web/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from PIL import Image
from selenium.common.exceptions import InvalidSessionIdException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webelement import WebElement
Expand Down Expand Up @@ -61,14 +62,15 @@ def __init__(self, headless=False):
self._clipboard = ""

# Stub mouse coordinates
self._html_elem = None
self._x = 0
self._y = 0

# State for Key modifiers
self._shift_hold = False

self._dimensions = (1600, 900)
self._download_folder_path = os.path.join(os.path.expanduser("~"), "Desktop")
self._download_folder_path = os.getcwd()

@property
def driver(self):
Expand All @@ -94,6 +96,7 @@ def driver_path(self, driver_path):
driver_path (str): The full path to the proper webdriver path used for the selected browser.
If set to None, the code will look into the PATH for the proper file when starting the browser.
"""
driver_path = os.path.abspath(os.path.expanduser(os.path.expandvars(driver_path)))
if driver_path and not os.path.isfile(driver_path):
raise ValueError("Invalid driver_path. The file does not exist.")
self._driver_path = driver_path
Expand Down Expand Up @@ -943,14 +946,17 @@ def print_pdf(self, path=None, print_options=None):
str: the saved file path
"""
title = self.page_title() or "document"
default_path = os.path.expanduser(os.path.join("~", "Desktop", f"{title}.pdf"))
timeout = 60000
if not self.page_title():
timeout = 1000
default_path = os.path.expanduser(os.path.join(self.download_folder_path, f"{title}.pdf"))

if self.browser in [Browser.CHROME, Browser.EDGE] and not self.headless:
# Chrome still does not support headless webdriver print
# but Firefox does.
self.execute_javascript("window.print();")
# We need to wait for the file to be available in this case.
self.wait_for_file(default_path)
self.wait_for_file(default_path, timeout=timeout)
return default_path

if print_options is None:
Expand All @@ -973,6 +979,7 @@ def print_pdf(self, path=None, print_options=None):
def wait_for_downloads(self, timeout: int = 120000):
"""
Wait for all downloads to be finished.
Beware that this method replaces the current page with the downloads window.
Args:
timeout (int, optional): Timeout in millis. Defaults to 120000.
Expand All @@ -994,7 +1001,7 @@ def find_elements(self, selector: str, by: By = By.CSS_SELECTOR) -> List[WebElem
**Example:**
```python
from botcity.web.bot import By
from botcity.web import By
...
# Find element by ID
all_cells = self.find_elements("//td", By.XPATH)
Expand All @@ -1017,7 +1024,7 @@ def find_element(self, selector: str, by: str = By.CSS_SELECTOR) -> WebElement:
**Example:**
```python
from botcity.web.bot import By
from botcity.web import By
...
# Find element by ID
elem = self.find_element("my_elem", By.ID)
Expand Down Expand Up @@ -1106,7 +1113,20 @@ def mouse_move(self, x, y):
y (int): The Y coordinate
"""
# ActionChains(self._driver).move_by_offset(-self._x, -self._y).perform()
if self.browser == Browser.FIREFOX:
# Reset coordinates if the page has gone stale. Only required for Firefox
if self._html_elem is None:
self._html_elem = self._driver.find_element_by_tag_name('body')
self._x = 0
self._y = 0
else:
try:
self._html_elem.is_enabled()
except StaleElementReferenceException:
self._html_elem = self._driver.find_element_by_tag_name('body')
self._x = 0
self._y = 0

mx = x - self._x
my = y - self._y
self._x = x
Expand Down Expand Up @@ -1798,6 +1818,7 @@ def wait_for_file(self, path, timeout=60000):
status (bool): Whether or not the file was available before the timeout
"""
path = os.path.abspath(os.path.expanduser(os.path.expandvars(path)))
start_time = time.time()

while True:
Expand Down
6 changes: 4 additions & 2 deletions botcity/web/browsers/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
chrome_options.add_argument(f"--user-data-dir={user_data_dir}")

if not download_folder_path:
download_folder_path = os.path.join(os.path.expanduser("~"), "Desktop")
download_folder_path = os.getcwd()

app_state = {
'recentDestinations': [{
Expand All @@ -67,7 +67,9 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
"kind": "local",
"namePattern": "Save as PDF",
},
"safebrowsing.enabled": True
"safebrowsing.enabled": True,
"credentials_enable_service": False,
"profile.password_manager_enabled": False
}

chrome_options.add_experimental_option("prefs", prefs)
Expand Down
6 changes: 4 additions & 2 deletions botcity/web/browsers/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
edge_options.add_argument(f"--user-data-dir={user_data_dir}")

if not download_folder_path:
download_folder_path = os.path.join(os.path.expanduser("~"), "Desktop")
download_folder_path = os.getcwd()

app_state = {
"recentDestinations": [{
Expand All @@ -70,7 +70,9 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
"kind": "local",
"namePattern": "Save as PDF",
},
"safebrowsing.enabled": True
"safebrowsing.enabled": True,
"credentials_enable_service": False,
"profile.password_manager_enabled": False
}

edge_options.add_experimental_option("prefs", prefs)
Expand Down
4 changes: 1 addition & 3 deletions botcity/web/browsers/firefox.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
firefox_profile.set_preference('browser.download.folderList', 2)
firefox_profile.set_preference('browser.download.manager.showWhenStarting', False)
if not download_folder_path:
download_folder_path = os.path.join(os.path.expanduser("~"), "Desktop")
download_folder_path = os.getcwd()
firefox_profile.set_preference('browser.download.dir', download_folder_path)
firefox_profile.set_preference('general.warnOnAboutConfig', False)

Expand All @@ -362,9 +362,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non


def wait_for_downloads(driver):
print('Start Wait for Downloads')
if not driver.current_url.startswith("about:downloads"):
print('Open the downloads tab')
driver.get("about:downloads")

return driver.execute_script("""
Expand Down

0 comments on commit a1245ce

Please sign in to comment.