python - 无论如何要使用现有的浏览器会话来运行 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")
解决方案
类型 :
**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)
推荐阅读
- nvidia-jetson - 我们可以将 azure kinect DK 与 Nvidia Jetson Tx2 开发套件连接起来吗?
- javascript - 将 base64 编码文件从客户端 JS 传递到服务器端 JS 以进行 API 调用
- node.js - 如何使用车把比较结果?
- c++ - 构建可组合的有向图(用于扫描仪生成器的 Thompson 构建算法)
- javascript - Max-Mn Sum JavaScript HackerRank 挑战
- google-photos - 从相册中选择一些照片并创建一个 url 以使用 Google Photo API 共享它们
- flutter - 我怎样才能得到2次之间的差异?
- debian - 如何让 debian 看到非免费固件文件?
- artificial-intelligence - 如何从 AIML 中的特定映射值中获取键值?
- python - 低级鼠标位置