1

I have been using Selenium for a couple of years now. I learned a ton, thanks to all the amazing people here at SO. Now, I just encountered a new challenge/opportunity. It seems like Selenium always wants to open a new window, which is a new instance, as far as I can tell, and this new instance doesn't know that I am already logged into the system that I want to be logged into. Normally I would run some generic code like this.

import time
from selenium.webdriver import ActionChains
from selenium import webdriver
driver = webdriver.Chrome('C:\\Users\\chromedriver.exe')

driver.get ('https://corp-login_place')
driver.find_element_by_id('USERID').send_keys('myid')
driver.find_element_by_id ('PASSWORD').send_keys('mypw')
    
list = [145, 270, 207]
for i in list:
    driver.find_element_by_id('InputKeys_LOCATION').send_keys(i)
    
    driver.find_element_by_class('PSPUSHBUTTON').click()
    button = driver.find_element_by_class_name("Button")
    button.click()
    time.sleep(5)

So, I think that automation script should work, but when I run the code, it always opens a new browser window, and asks me to login. At this point my login credential don't work. Very weird. Anyway, I'm already logged in, and everything works fine if I just use the mouse and keyboard. It seems like the open browser window, which is out of focus, works fine, but Selenium don't focus on ths open window, and it opens a new window, which doesn't allow me to login.

For some reason, the code doesn't work when I hit F9, but if I run the process manually, using the mouse and keyboard, everything works totally fine. I feel like

Any insight or illumination, as to what is happening here, would be greatly appreciated. Thanks to all!

ASH
  • 18,040
  • 13
  • 61
  • 153
  • I assume that the spaces in `driver.get ('https://corp-login_place')` and `driver.find_element_by_id ('PASSWORD').send_keys('mypw')` are typos, and you have those fixed when you are running? – C. Peck May 12 '21 at 01:18

2 Answers2

0

What I got is that you have a manually opened browser window (which was not opened with Selenium) and you want to control this window with Selenium?

Apparently this is not supported, but there is some code here that maybe can help you.

0

Selenium always uses a new instance of the browser no matter how many other browser windows are open in your system. If your problem is related to logging into the website you want to test, then please share the website url.
Or else if you are talking about attaching a session of browser window already opened in your system then this article (http://tarunlalwani.com/post/reusing-existing-browser-session-selenium/) might help, albeit the whole purpose of automating is compromised.

Libin Thomas
  • 461
  • 4
  • 11