1

I am running Ubuntu 14.04, Firefox 49.0.2, Python 3.4.3 & Selenium 3.0.1

I want to use Selenium to automate some browser functions, not to do any web site testing. How can I can I modify the simple login script below to use the instance of Firefox running on my desktop instead of opening a new Firefox window?

# login_yahoo_mail.py

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.get('http://mail.yahoo.com/?.intl=us')
enter_email = driver.find_element_by_id('login-username')
enter_email.clear()
enter_email.send_keys('cleanman2@yahoo.com')
next_button = driver.find_element_by_id('login-signin')
next_button.click()
enter_password = WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID, 'login-passwd')))
enter_password.send_keys('dumba$$yahoo!^!&')
signin_button = driver.find_element_by_id('login-signin')
signin_button.click()

Thanks, Jim

Cimbali
  • 11,012
  • 1
  • 39
  • 68
Agent_15x
  • 373
  • 4
  • 13
  • If the purposes aren't for testing, there are easier ways to automate browser functions on an already open instance using javascript directly, for example Greasemonkey. – yoonjesung Nov 04 '16 at 20:24
  • I haven't used javascript or Greasemonkey for a long time and don't want to relearm them right now. I would prefer using Python if possible. – Agent_15x Nov 04 '16 at 20:35
  • Before anyone tells me, I just changed my password. I had to repaste the code and forgot to munge the log on info the second time. Dumb mistake, my apologies. – Agent_15x Nov 04 '16 at 20:44
  • Possible duplicate of [How to use/attach an existing browser using Selenium?](http://stackoverflow.com/questions/26386418/how-to-use-attach-an-existing-browser-using-selenium) – OCary Nov 04 '16 at 20:49
  • I looked at them before asking this question and they did not help me. They talk of remote servers, session id's and java patches. I am trying to determine if I can use Firefox running on my desktop. If I open a Firefox window I don't think there is a session id to attach to at that point, maybe I am wrong. – Agent_15x Nov 04 '16 at 22:29

1 Answers1

-1

It is possible with 'Selenium 4'.

Each window has a unique identifier which remains persistent in a single session. You can get the window handle of the current window by using: driver.current_window_handle

driver = webdriver.Firefox()

# Store the ID of the original window
original_window = driver.current_window_handle

# Opens a new tab and switches to new tab
driver.switch_to.new_window('tab')

# Opens a new window and switches to new window
driver.switch_to.new_window('window')

#Close the tab or window
driver.close()

#Switch back to the old tab or window
driver.switch_to.window(original_window)

I haven't try it yet but since Webdriver doesn't know where the OS focus is, you can find the way you want with those options.

More: Windows and tabs handle

Facu
  • 1
  • 2