Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
Fix import error seen on some Windows machines and handle command key…
Browse files Browse the repository at this point in the history
… for Mac
  • Loading branch information
coskundeniz committed Dec 19, 2023
1 parent ca0937f commit 4a855c8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
10 changes: 9 additions & 1 deletion proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@

from pathlib import Path

from selenium.webdriver import ChromeOptions
try:
from selenium.webdriver import ChromeOptions
except ImportError:
import sys

packages_path = Path.cwd() / "env" / "Lib" / "site-packages"
sys.path.insert(0, f"{packages_path}")

from selenium.webdriver import ChromeOptions


def get_proxies(proxy_file: Path) -> list[str]:
Expand Down
28 changes: 25 additions & 3 deletions search_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-- Özdemir Asaf
"""

import sys
import random
from time import sleep

Expand Down Expand Up @@ -127,6 +128,10 @@ def click_ads(self, ads: AdList) -> None:
self._driver.find_element(By.TAG_NAME, "body").send_keys(Keys.HOME)
sleep(1)

platform = sys.platform

control_command_key = Keys.COMMAND if platform.endswith("darwin") else Keys.CONTROL

# store the ID of the original window
original_window_handle = self._driver.current_window_handle

Expand All @@ -140,12 +145,30 @@ def click_ads(self, ads: AdList) -> None:
# open link in a different tab
actions = ActionChains(self._driver)
actions.move_to_element(ad_link_element)
actions.key_down(Keys.CONTROL)
actions.key_down(control_command_key)
actions.click()
actions.key_up(Keys.CONTROL)
actions.key_up(control_command_key)
actions.perform()
sleep(0.5)

if len(self._driver.window_handles) != 2:
logger.debug("Couldn't click! Scrolling element into view...")

self._driver.execute_script(
"arguments[0].scrollIntoView(true);", ad_link_element
)

actions = ActionChains(self._driver)
actions.move_to_element(ad_link_element)
actions.key_down(control_command_key)
actions.click()
actions.key_up(control_command_key)
actions.perform()
sleep(0.5)

else:
logger.debug("Opened link in new a tab. Switching to tab...")

for window_handle in self._driver.window_handles:
if window_handle != original_window_handle:
self._driver.switch_to.window(window_handle)
Expand Down Expand Up @@ -181,7 +204,6 @@ def end_search(self) -> None:

self._driver = None


def _load(self) -> None:
"""Load Google main page"""

Expand Down

0 comments on commit 4a855c8

Please sign in to comment.