3

I have multiple FireFox profiles and I want to open a profile, open a few tabs with diffrent URLs, open another profile open tabs with URLs. For some reason send_keys does not seem to work, but window.open does. This is my code, so far.

import os
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
p1 = webdriver.FirefoxProfile(profile_directory="C:/Users/User/AppData/Roaming/Mozilla/Firefox/Profiles/4yopmm8r.py")
driver = webdriver.Firefox(firefox_profile=p1)
driver.get("https://www.reddit.com/")
time.sleep(5)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + "t")
driver.get("https://www.stackoverflow.com/")

All it does is open reddit, wait 5 seconds and then open stackoverflow. How do I fix this?

kufte
  • 31
  • 2
  • its firefox that needs to get the signal, not the body i think ... i have no idea how you would do that ... driver.send_keys ? – Joran Beasley Mar 14 '19 at 01:29
  • So which is the problem: **1.** It is not accessing your profile on reddit; or **2.** The new tab is not opening, it's just changing the URL of the current tab? – C. Peck Mar 14 '19 at 01:33
  • @C.Peck Yes, it is just changing the URL of the current tab. – kufte Mar 14 '19 at 01:40

3 Answers3

1

If CTRL+t isn't working for you try the following:

driver.get("https://www.reddit.com/")
windows_before  = driver.current_window_handle
driver.execute_script("window.open('https://www.stackoverflow.com/')")
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
windows_after = driver.window_handles

Then, if you want to switch back to the original tab,

new_window = [x for x in windows_after if x != windows_before][0]
driver.switch_to_window(new_window)
C. Peck
  • 3,641
  • 3
  • 19
  • 36
0

Try to open new tab using script instead of Ctrl + t keys, then change the focus using switch_to_window function.

from selenium import webdriver
import time

p1 = webdriver.FirefoxProfile(profile_directory="C:/Users/User/AppData/Roaming/Mozilla/Firefox/Profiles/4yopmm8r.py")
driver = webdriver.Firefox(firefox_profile=p1)
driver.get("https://www.reddit.com/")
time.sleep(5)
# Open a new window
# This does not change focus to the new window for the driver.
driver.execute_script("window.open('');")
time.sleep(3)
# Switch to the new window
driver.switch_to_window(driver.window_handles[-1])
driver.get("https://www.stackoverflow.com/")

or try to use your original code, but use send_keys(Keys.CONTROL + "T") (capital) instead of send_keys(Keys.CONTROL + "t").

Hiadore
  • 686
  • 3
  • 15
  • It works, but this is not what I need. For some reason on FireFox this opens a whole new [browser](https://i.imgur.com/Z1kqMI4.png), not a new [tab](https://i.imgur.com/SeRMou6.png). I need to open tabs too keep somekind of order – kufte Mar 14 '19 at 01:44
  • Why do you switch to `window_handles[**-1**]? – C. Peck Mar 14 '19 at 01:44
  • 2
    @C.Peck `window_handles[-1]` refer to last window created. You can use `window_handles[0]` which refer to first window, etc. – Hiadore Mar 14 '19 at 02:12
  • good to know, thanks – C. Peck Mar 14 '19 at 02:13
  • @kufte, in your original code, after `send_keys`, please add `time.sleep` and then `switch_to_window` before get new url. Also, if you use mac use `Keys.COMMAND` instead of `Keys.CONTROL`. – Hiadore Mar 14 '19 at 02:19
  • looking at the profile directory I'm thinking windows :) – C. Peck Mar 14 '19 at 02:25
0

You can open the tab using the execute_script. Here is the sample code.

#navigate to reddit in base tab
driver.get("https://www.reddit.com/")
time.sleep(5) # actually you can wait for one of the element present status.
base_tab = driver.window_handles[0]

#open the new tab and navigate to SO
driver.execute_script("window.open('https://www.stackoverflow.com/')")
latest_tab = driver.window_handles[-1]

# use .swith_to.window to access the desired tab
driver.switch_to.window(base_tab)
driver.switch_to.window(latest_tab)

Other way to access the tab by index Using driver.window_handles, which will give you the list of windows. Now you can choose the tabs by index (starts with 0)

# base tab
driver.switch_to.window(driver.window_handles[0])
# second tab
driver.switch_to.window(driver.window_handles[1])
# latest tab
driver.switch_to.window(driver.window_handles[-1])

It's important to make sure the first window is loaded completely before opening the new tab, otherwise the window_handles does not match the order you expect. Because window_handles will consider the tab only once it's completely loaded.

supputuri
  • 13,644
  • 2
  • 21
  • 39