简体   繁体   中英

Attach Selenium Chrome webdriver to already opened browser window?

For example some user launched his browser, then he launched my program. I need to interact with this browser (not necessarily Chrome). Is there a solution for my problem in Selenium ? Or in another way?

1) I saw the almost suitable way. It is to launch Chrome from Command Prompt by this command:

chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\ChromeProfile"

And then to attach launched browser in code like this:

# PYTHON Example
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "localhost:1234")
#Change chrome driver path accordingly
chrome_driver = "C:\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver, chrome_options=chrome_options)

But this is a manually launched browser

2) Or the second way at this link . But there is problem from the first try.

3) In this issue on GitHub I found this class , but it doesn't work for me. In the issue was written that it was tested only with Firefox (on ubuntu).

Is there a solution? Thanks in advance.

This is a very hacky solution considering Software development practices but here goes..

Once you initiate Google chrome manually or with a script, you can interact with that using PyAutoGUI module. We can move the mouse to a position on the screen, click and then type.

An example code after opening chrome:

import pyautogui, time
pyautogui.PAUSE = 2

# x and y is the mouse position on your screen    
pyautogui.moveTo(x=410, y=79)
pyautogui.click()
# select all
pyautogui.hotkey('ctrl', 'a')
pyautogui.press(['delete'])
time.sleep(2)
pyautogui.typewrite("https://google.com")
pyautogui.press('enter')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM