How to change Setup of "Selenium Python config For Chrome" to Firefox and Edge Browser?

107 views Asked by At

can anyone kindly help me change/rewrite my working Selenium Python code working on chrome to Firefox and Edge?

i can't seem to find solution and rewrite importing firefox or edge drivers on the code myself.

this is the code below thanks <3

import undetected_chromedriver as uc
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
import re
chrome_options = Options()
# chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument("User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
# chrome_options.add_argument('--headless')
chrome_options.headless = False
driver = uc.Chrome(options=chrome_options,use_subprocess=True)



accounts = []
with open('acc.txt', encoding='utf-8') as f:
    for account in f.readlines():
        account = account.split('\n')[0].split('<>')
        accounts.append({
            'username': account[0],
            'password': account[1]
        })
invalid = []
valid = []
for account in accounts:
    try:
        driver.get("https://URL/appsuite/")
        time.sleep(3)
        username = driver.find_element(By.ID, 'io-ox-login-username')
        username.send_keys(account['username'])
        time.sleep(1)
        continue_button = driver.find_element(By.ID, 'io-ox-login-button')
        continue_button.click()
        time.sleep(1)
        password = driver.find_element(By.ID, 'io-ox-login-password')
        password.send_keys(account['password'])
        time.sleep(1)

        login_button = driver.find_element(By.ID, 'io-ox-login-button')
        time.sleep(1)
        login_button.click()

        time.sleep(2)
        print(driver.title)
        if(driver.title != 'Sign in - Professional Email'):
            with open('valid.txt', 'a', encoding='utf-8') as f:
                f.write(f"{account['username']}<>{account['password']}\n")
        else:
            with open('invalid.txt', 'a', encoding='utf-8') as f:
                f.write(f"{account['username']}<>{account['password']}\n")
        time.sleep(1)        
        driver.delete_all_cookies()
        time.sleep(1)
        driver.delete_all_cookies()
    except Exception as e:
        print(e)

driver.quit()

Thanks.

1

There are 1 answers

4
SSujitXX On

undetected_chromedriver is for chrome only, you have to use selenium raw.

For FireFox

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options as FirefoxOptions
import time
import re

firefox_options = FirefoxOptions()
firefox_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
firefox_options.headless = False

driver = webdriver.Firefox(options=firefox_options)

accounts = []
with open('acc.txt', encoding='utf-8') as f:
    for account in f.readlines():
        account = account.split('\n')[0].split('<>')
        accounts.append({
            'username': account[0],
            'password': account[1]
        })

invalid = []
valid = []

for account in accounts:
    try:
        driver.get("https://URL/appsuite/")
        time.sleep(3)
        username = driver.find_element(By.ID, 'io-ox-login-username')
        username.send_keys(account['username'])
        time.sleep(1)
        continue_button = driver.find_element(By.ID, 'io-ox-login-button')
        continue_button.click()
        time.sleep(1)
        password = driver.find_element(By.ID, 'io-ox-login-password')
        password.send_keys(account['password'])
        time.sleep(1)
        login_button = driver.find_element(By.ID, 'io-ox-login-button')
        time.sleep(1)
        login_button.click()
        time.sleep(2)
        print(driver.title)
        if driver.title != 'Sign in - Professional Email':
            with open('valid.txt', 'a', encoding='utf-8') as f:
                f.write(f"{account['username']}<>{account['password']}\n")
        else:
            with open('invalid.txt', 'a', encoding='utf-8') as f:
                f.write(f"{account['username']}<>{account['password']}\n")
        time.sleep(1)
        driver.delete_all_cookies()
        time.sleep(1)
    except Exception as e:
        print(e)

driver.quit()

For Edge

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options as EdgeOptions
import time
import re

edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
edge_options.headless = False

driver = webdriver.Edge(options=edge_options)

accounts = []
with open('acc.txt', encoding='utf-8') as f:
    for account in f.readlines():
        account = account.split('\n')[0].split('<>')
        accounts.append({
            'username': account[0],
            'password': account[1]
        })

invalid = []
valid = []

for account in accounts:
    try:
        driver.get("https://URL/appsuite/")
        time.sleep(3)
        username = driver.find_element(By.ID, 'io-ox-login-username')
        username.send_keys(account['username'])
        time.sleep(1)
        continue_button = driver.find_element(By.ID, 'io-ox-login-button')
        continue_button.click()
        time.sleep(1)
        password = driver.find_element(By.ID, 'io-ox-login-password')
        password.send_keys(account['password'])
        time.sleep(1)
        login_button = driver.find_element(By.ID, 'io-ox-login-button')
        time.sleep(1)
        login_button.click()
        time.sleep(2)
        print(driver.title)
        if driver.title != 'Sign in - Professional Email':
            with open('valid.txt', 'a', encoding='utf-8') as f:
                f.write(f"{account['username']}<>{account['password']}\n")
        else:
            with open('invalid.txt', 'a', encoding='utf-8') as f:
                f.write(f"{account['username']}<>{account['password']}\n")
        time.sleep(1)
        driver.delete_all_cookies()
        time.sleep(1)
    except Exception as e:
        print(e)

driver.quit()