2

I have been using Selenium for a couple of years now. 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. What is happening here?

halfer
  • 19,824
  • 17
  • 99
  • 186
ASH
  • 20,759
  • 19
  • 87
  • 200
  • 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

1 Answers1

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
  • 829
  • 9
  • 18