首页 > 解决方案 > 无论如何要使用现有的浏览器会话来运行 selenium 会话

问题描述

我想知道是否有办法在当前的浏览器会话中运行 selenium?目前,我有一个使用 selenium 的脚本,但是一旦登录,该站点就会发送一个双重身份验证,因为浏览器似乎是一个新的登录点。我想要只更新字段而无需登录的脚本,只需打开已登录 chrome 或 firefox 的当前浏览器的新选项卡:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# driver = webdriver.Firefox()
# driver = webdriver.Firefox(executable_path=executable_path, firefox_options=options, log_path=os.devnull)

url="https://robinhood.com/"
driver = webdriver.Chrome("driver location")

driver.get(url)

loginBtn = driver.find_element_by_class_name('css-1tfdro-UnstyledAnchor-Button')

loginBtn.click()

# username = driver.find_element_by_name('username')


def login_rh(usernamef, username, passwordf, password):
    username_field = WebDriverWait(driver, 10). until(EC.presence_of_element_located((By.NAME, usernamef)))
    username_field.send_keys(username)

    password_field = WebDriverWait(driver, 10). until(EC.presence_of_element_located((By.CLASS_NAME, passwordf)))
    password_field.send_keys(password)

    submit_button= WebDriverWait(driver, 10). until(EC.presence_of_element_located((By.CLASS_NAME, "class")))
    submit_button.click()

login_rh("username", "test@gmail.com", "css-a4852m", "passtest")

标签: pythonselenium

解决方案


类型 :

**chrome://version/** in browser you will get executable path of chrome.exe

复制它并关闭所有 chrome 实例。

现在开始边缘为:

"C:\Program Files (x86)\Microsoft\chrome\Application\chrome.exe" --remote-debugging-port=5555

现在将远程调试端口添加为:

options = webdriver.ChromeOptions()

options.add_experimental_option("debuggerAddress", "127.0.0.1:5555");

webdriver.Chrome(options=options)

推荐阅读