Skip to content

Commit

Permalink
Move firefox extension loading process to driver creation method
Browse files Browse the repository at this point in the history
  • Loading branch information
david96182 committed May 3, 2024
1 parent dd36b81 commit 206b7b3
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 71 deletions.
21 changes: 1 addition & 20 deletions ninjemail/email_providers/outlook.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import logging
import os
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.remote.webelement import WebElement
from selenium import webdriver
import undetected_chromedriver as uc

URL = 'https://signup.live.com/signup'
WAIT = 25

def create_account(captcha_key,
driver,
def create_account(driver,
username,
password,
first_name,
Expand All @@ -27,7 +23,6 @@ def create_account(captcha_key,
Automatically creates an outlook/hotmail account.
Args:
captcha_key (str): The API key for Death By Captcha service in the format "username:password".
driver (WebDriver): The Selenium WebDriver instance for the configured browser.
username (str): The desired username for the email account.
password (str): The desired password for the email account.
Expand All @@ -45,20 +40,6 @@ def create_account(captcha_key,
"""
logging.info('Creating outlook account')

if type(driver) is webdriver.Firefox:
driver.install_addon(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'captcha_solvers/capsolver_captcha_solver-1.10.4.xpi'))
driver.get('https://www.google.com')
capsolver_src = driver.find_element(By.XPATH, '/html/script[2]')
capsolver_src = capsolver_src.get_attribute('src')
capsolver_ext_id = capsolver_src.split('/')[2]
driver.get(f'moz-extension://{capsolver_ext_id}/www/index.html#/popup')
time.sleep(5)

api_key_input = driver.find_element(By.XPATH, '//input[@placeholder="Please input your API key"]')
api_key_input.send_keys(captcha_key)
driver.find_element(By.ID, 'q-app').click()
time.sleep(5)

driver.get(URL)

# Select create email
Expand Down
25 changes: 1 addition & 24 deletions ninjemail/email_providers/yahoo.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import logging
import os
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from sms_services import getsmscode, smspool, fivesim
import undetected_chromedriver as uc

URL = 'https://login.yahoo.com/account/create'
WAIT = 25

def create_account(captcha_key,
driver,
def create_account(driver,
sms_key,
username,
password,
Expand All @@ -28,7 +24,6 @@ def create_account(captcha_key,
Automatically creates a Yahoo account.
Args:
captcha_key (str): The API key for the captcha solving service.
driver (WebDriver): The Selenium WebDriver instance for the configured browser.
sms_key(dict): The data of the SMS service.
username (str): The desired username for the email account.
Expand Down Expand Up @@ -60,24 +55,6 @@ def create_account(captcha_key,
data.update({'service': 'yahoo'})
sms_provider = fivesim.FiveSim(**data)

if type(driver) is webdriver.Firefox:
driver.install_addon(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'captcha_solvers/capsolver_captcha_solver-1.10.4.xpi'))
driver.get('https://www.google.com')
capsolver_src = driver.find_element(By.XPATH, '/html/script[2]')
capsolver_src = capsolver_src.get_attribute('src')
capsolver_ext_id = capsolver_src.split('/')[2]
driver.get(f'moz-extension://{capsolver_ext_id}/www/index.html#/popup')
time.sleep(5)

api_key_input = driver.find_element(By.XPATH, '//input[@placeholder="Please input your API key"]')
api_key_input.send_keys(captcha_key)
driver.find_element(By.ID, 'q-app').click()

# solve recaptcha by token
#token_recaptcha = driver.find_element(By.XPATH, '/html/body/div/div/div/main/div[2]/div[2]/div[1]/div[1]/div[3]/div[2]/div[1]/div[1]/span')
#token_recaptcha.click()
time.sleep(5)

logging.info('Creating Yahoo account')

driver.get(URL)
Expand Down
6 changes: 2 additions & 4 deletions ninjemail/ninjemail_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ def create_outlook_account(self,
country, birthdate = generate_missing_info(username, password, first_name, last_name, country, birthdate)
month, day, year = get_birthdate(birthdate)

return outlook.create_account(captcha_key,
driver,
return outlook.create_account(driver,
username,
password,
first_name,
Expand Down Expand Up @@ -273,8 +272,7 @@ def create_yahoo_account(self,
_, birthdate = generate_missing_info(username, password, first_name, last_name, '', birthdate)
month, day, year = get_birthdate(birthdate)

return yahoo.create_account(captcha_key,
driver,
return yahoo.create_account(driver,
sms_key,
username,
password,
Expand Down
9 changes: 3 additions & 6 deletions ninjemail/tests/test_email_outlook.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def test_create_account_firefox():

driver = create_driver('firefox')
# Test data
captcha_key= "4564654777"
username = "testuser"
password = "testpassword"
first_name = "John"
Expand All @@ -84,15 +83,14 @@ def test_create_account_firefox():
year = "2000"
hotmail = False

email, password = create_account(captcha_key, driver, username, password, first_name, last_name, country, month, day, year, hotmail)
email, password = create_account(driver, username, password, first_name, last_name, country, month, day, year, hotmail)
assert email == f"{username}@outlook.com"
assert password == "testpassword"

def test_create_account_chrome():

driver = create_driver('chrome')
# Test data
captcha_key="5456646656"
username = "testuser"
password = "testpassword"
first_name = "John"
Expand All @@ -103,7 +101,7 @@ def test_create_account_chrome():
year = "2000"
hotmail = False

email, password = create_account(captcha_key, driver, username, password, first_name, last_name, country, month, day, year, hotmail)
email, password = create_account(driver, username, password, first_name, last_name, country, month, day, year, hotmail)
assert email == f"{username}@outlook.com"
assert password == "testpassword"

Expand All @@ -112,7 +110,6 @@ def test_create_hotmail_account():
driver = create_driver('chrome')
# Test data

captcha_key="5456646656"
username = "testuser"
password = "testpassword"
first_name = "John"
Expand All @@ -123,7 +120,7 @@ def test_create_hotmail_account():
year = "2000"
hotmail = True

email, password = create_account(captcha_key, driver, username, password, first_name, last_name, country, month, day, year, hotmail)
email, password = create_account(driver, username, password, first_name, last_name, country, month, day, year, hotmail)
assert email == f"{username}@hotmail.com"
assert password == "testpassword"

12 changes: 4 additions & 8 deletions ninjemail/tests/test_email_yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def test_create_account_firefox_and_getsmscode(monkeypatch):

driver = create_driver('firefox')
# Test data
captcha_key= "4564654777"
sms_key = {
"name": "getsmscode",
"data": {
Expand All @@ -97,7 +96,7 @@ def test_create_account_firefox_and_getsmscode(monkeypatch):
year = "2000"
myyahoo = False

email, password = create_account(captcha_key, driver, sms_key, username, password, first_name, last_name, month, day, year, myyahoo)
email, password = create_account(driver, sms_key, username, password, first_name, last_name, month, day, year, myyahoo)
assert email == f"{username}@yahoo.com"
assert password == "testpassword"

Expand All @@ -113,7 +112,6 @@ def test_create_account_chrome_and_smspool(monkeypatch):
"token": "your_api_key",
}
}
captcha_key="5456646656"
username = "testuser"
password = "testpassword"
first_name = "John"
Expand All @@ -123,7 +121,7 @@ def test_create_account_chrome_and_smspool(monkeypatch):
year = "2000"
myyahoo = False

email, password = create_account(captcha_key, driver, sms_key, username, password, first_name, last_name, month, day, year, myyahoo)
email, password = create_account(driver, sms_key, username, password, first_name, last_name, month, day, year, myyahoo)
assert email == f"{username}@yahoo.com"
assert password == "testpassword"

Expand All @@ -133,7 +131,6 @@ def test_create_account_firefox_and_fivesim(monkeypatch):

driver = create_driver('firefox')
# Test data
captcha_key= "4564654777"
sms_key = {
"name": "5sim",
"data": {
Expand All @@ -150,7 +147,7 @@ def test_create_account_firefox_and_fivesim(monkeypatch):
year = "2000"
myyahoo = False

email, password = create_account(captcha_key, driver, sms_key, username, password, first_name, last_name, month, day, year, myyahoo)
email, password = create_account(driver, sms_key, username, password, first_name, last_name, month, day, year, myyahoo)
assert email == f"{username}@yahoo.com"
assert password == "testpassword"

Expand All @@ -161,7 +158,6 @@ def test_create_myyahoo_account(monkeypatch):
driver = create_driver('chrome')
# Test data

captcha_key="5456646656"
sms_key = {
"name": "smspool",
"data": {
Expand All @@ -177,7 +173,7 @@ def test_create_myyahoo_account(monkeypatch):
year = "2000"
myyahoo = True

email, password = create_account(captcha_key, driver, sms_key, username, password, first_name, last_name, month, day, year, myyahoo)
email, password = create_account(driver, sms_key, username, password, first_name, last_name, month, day, year, myyahoo)
assert email == f"{username}@myyahoo.com"
assert password == "testpassword"

Expand Down
14 changes: 7 additions & 7 deletions ninjemail/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def mock_chrome_quit(self):

def test_create_outlook_account():

manager = Ninjemail(captcha_keys={'capsolver': {'token': 'aaaaaaaa'}})
manager = Ninjemail(browser='chrome', captcha_keys={'capsolver': {'token': 'aaaaaaaa'}})
username, password = manager.create_outlook_account(
username="testuser",
password="testpassword",
Expand All @@ -157,7 +157,7 @@ def test_create_outlook_account():

def test_create_outlook_account_no_info():

manager = Ninjemail(captcha_keys={'capsolver': {'token': 'aaaaaaaa'}})
manager = Ninjemail(browser='chrome', captcha_keys={'capsolver': {'token': 'aaaaaaaa'}})
username, password = manager.create_outlook_account(
)

Expand All @@ -166,7 +166,7 @@ def test_create_outlook_account_no_info():

def test_create_outlook_account_with_proxy():

manager = Ninjemail(captcha_keys={'capsolver': {'token': 'aaaaaaaa'}}, proxy='http://127.0.0.1:8080')
manager = Ninjemail(browser='chrome', captcha_keys={'capsolver': {'token': 'aaaaaaaa'}}, proxy='http://127.0.0.1:8080')
username, password = manager.create_outlook_account(
)

Expand Down Expand Up @@ -236,7 +236,7 @@ def test_create_gmail_account_no_sms_key():

def test_create_yahoo_account():

manager = Ninjemail(captcha_keys={'capsolver': {'token': 'aaaaaaaa'}},
manager = Ninjemail(browser='chrome', captcha_keys={'capsolver': {'token': 'aaaaaaaa'}},
sms_keys={'smspool': {'token': 'bbbbbb'}})
username, password = manager.create_yahoo_account(
username="testuser",
Expand All @@ -251,7 +251,7 @@ def test_create_yahoo_account():

def test_create_yahoo_account_no_info():

manager = Ninjemail(captcha_keys={'capsolver': {'token': 'aaaaaaaa'}},
manager = Ninjemail(browser='chrome', captcha_keys={'capsolver': {'token': 'aaaaaaaa'}},
sms_keys={'smspool': {'token': 'bbbbbb'}})
username, password = manager.create_yahoo_account(
)
Expand All @@ -261,7 +261,7 @@ def test_create_yahoo_account_no_info():

def test_create_yahoo_account_with_proxy():

manager = Ninjemail(captcha_keys={'capsolver': {'token': 'aaaaaaaa'}},
manager = Ninjemail(browser='chrome', captcha_keys={'capsolver': {'token': 'aaaaaaaa'}},
sms_keys={'smspool': {'token': 'bbbbbb'}}, proxy='http://127.0.0.1:8080')
username, password = manager.create_yahoo_account(
)
Expand All @@ -285,7 +285,7 @@ def test_create_yahoo_account_no_captcha_key():

def test_create_yahoo_account_no_sms_key():

manager = Ninjemail(captcha_keys={'capsolver': {'token': 'aaaaaaaa'}})
manager = Ninjemail(browser='chrome', captcha_keys={'capsolver': {'token': 'aaaaaaaa'}})
with pytest.raises(ValueError) as excinfo:
manager.create_yahoo_account(
username="testuser",
Expand Down
17 changes: 15 additions & 2 deletions ninjemail/tests/test_webdriver_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from unittest.mock import MagicMock
import pytest
from selenium.webdriver import Firefox, Chrome
import undetected_chromedriver as uc
Expand Down Expand Up @@ -28,6 +29,9 @@ def mock_firefox_quit(self):

def mock_chrome_quit(self):
pass

def mock_firefox_addon(*args, **kwargs):
pass

monkeypatch.setattr('webdriver_manager.firefox.GeckoDriverManager.install', mock_gecko_install)
monkeypatch.setattr('webdriver_manager.chrome.ChromeDriverManager.install', mock_chrome_install)
Expand All @@ -37,9 +41,14 @@ def mock_chrome_quit(self):
monkeypatch.setattr('selenium.webdriver.Chrome.__init__', mock_chrome)
monkeypatch.setattr('undetected_chromedriver.Chrome.__init__', mock_chrome)
monkeypatch.setattr('selenium.webdriver.Firefox.quit', mock_firefox_quit)
monkeypatch.setattr('selenium.webdriver.Firefox.install_addon', mock_firefox_addon)
monkeypatch.setattr('selenium.webdriver.Firefox.get', MagicMock())
monkeypatch.setattr('selenium.webdriver.Firefox.find_element', MagicMock())
monkeypatch.setattr('selenium.webdriver.Chrome.quit', mock_chrome_quit)
monkeypatch.setattr('undetected_chromedriver.Chrome.quit', mock_chrome_quit)

monkeypatch.setattr('time.sleep', MagicMock(()))


def test_create_firefox_driver_no_proxy_no_captcha():
driver = create_driver('firefox')
Expand All @@ -58,7 +67,7 @@ def test_create_undetected_chrome_driver():
driver.quit()

def test_create_undetected_chrome_driver_with_proxy_and_captcha():
driver = create_driver('undetected-chrome', captcha_extension=True, proxy='http://10.10.10.1:2020')
driver = create_driver('undetected-chrome', captcha_extension=True, proxy='http://10.10.10.1:2020', captcha_key='test_key')
assert isinstance(driver, uc.Chrome)
driver.quit()

Expand All @@ -79,10 +88,14 @@ def test_create_chrome_driver_with_proxy():
assert isinstance(driver, Chrome)
driver.quit()

def test_create_chrome_driver_with_proxy_and_captcha():
driver = create_driver('chrome', captcha_extension=True, proxy='http://10.10.10.1:2020', captcha_key='test_key')
assert isinstance(driver, Chrome)
driver.quit()

def test_create_firefox_driver_with_captcha_extension():

driver = create_driver('firefox', captcha_extension=True)
driver = create_driver('firefox', captcha_extension=True, captcha_key='test_key')
assert isinstance(driver, Firefox)
driver.quit()

Expand Down
16 changes: 16 additions & 0 deletions ninjemail/utils/webdriver_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.common.by import By
import undetected_chromedriver as uc
import os
from urllib.parse import urlparse
import os
import re
import time

def add_capsolver_api_key(file_path, api_key):
with open(file_path, 'r') as file:
Expand Down Expand Up @@ -72,6 +74,20 @@ def create_driver(browser, captcha_extension=False, proxy=None, captcha_key=None
options.profile = custom_profile

driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()), options=options)

if captcha_extension:
driver.install_addon(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'captcha_solvers/capsolver_captcha_solver-1.10.4.xpi'))
driver.get('https://www.google.com')
capsolver_src = driver.find_element(By.XPATH, '/html/script[2]')
capsolver_src = capsolver_src.get_attribute('src')
capsolver_ext_id = capsolver_src.split('/')[2]
driver.get(f'moz-extension://{capsolver_ext_id}/www/index.html#/popup')
time.sleep(5)

api_key_input = driver.find_element(By.XPATH, '//input[@placeholder="Please input your API key"]')
api_key_input.send_keys(captcha_key)
driver.find_element(By.ID, 'q-app').click()

elif browser == 'chrome':
options = ChromeOptions()
options.add_argument('--no-sandbox')
Expand Down

0 comments on commit 206b7b3

Please sign in to comment.