I should be able to open a new tab in selenium for python using the code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")
body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.COMMAND + 't')
But no new tab opens, and no error message appears (http://stackoverflow.com/ does load).
Note that I am using Keys.COMMAND + 't' because I am running the code on OS X.
I have no idea what is causing the issue as posts like this one, indicate that my code should work.
Updated to include answers
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")
current_tab = driver.current_window_handle
driver.execute_script('window.open();')
new_tab = [tab for tab in driver.window_handles if tab != current_tab][0]
driver.switch_to.window(new_tab)
driver.get("http://github.com")
inputElement = driver.find_element_by_id("user[login]")
inputElement.send_keys('1')
current_tab = driver.current_window_handle
driver.execute_script('window.open();')
new_tab = [tab for tab in driver.window_handles if tab != current_tab][0]
driver.switch_to.window(new_tab)
driver.get("http://github.com")
inputElement = driver.find_element_by_id("user[email]")
inputElement.send_keys('2')
Try below code to open new tab and switch to it:
driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")
current_tab = driver.current_window_handle
driver.execute_script('window.open("http://github.com");')
new_tab = [tab for tab in driver.window_handles if tab != current_tab][0]
driver.switch_to.window(new_tab)
inputElement = driver.find_element_by_id("user[login]")
inputElement.send_keys('1')
driver.execute_script('window.open("http://github.com");')
third_tab = [tab for tab in driver.window_handles if tab not in (current_tab, new_tab)][0]
driver.switch_to.window(third_tab)
inputElement = driver.find_element_by_id("user[email]")
inputElement.send_keys('2')
You can use driver.close() to close new tab and driver.switch_to.window(current_tab) to switch back to initial tab
Also note that you can pass page URL you want to open in new tab as argument to window.open() like:
driver.execute_script('window.open("https://google.com");')
Try below code to open new tab in MAC:-
String clickOnTabLink = Keys.chord(Keys.COMMAND, "t", Keys.ENTER);
link.sendKeys(clickOnTabLink);
Related
I know how to open a new window using selenium. But I want a new tab in the same window of chrome which I am using. How to do that?
I already try:
from selenium import webdriver
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_experimental_option('debuggerAddress', 'localhost:9014')
driver = webdriver.Chrome(options=options)
try:
button = driver.find_element(By.CLASS_NAME, 'button')
button.click()
finally:
driver.quit()
You can achieve the opening/closing of a tab by the combination of keys:
Ctrl + T or Ctrl + W
You can trigger this shortcut on body of the page, to open new tab within the window, like this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.google.com/")
#open tab
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
# You can use (Keys.CONTROL + 't') on other OSs
# Load a page
driver.get('http://stackoverflow.com/')
# Make the tests...
# close the tab
# (Keys.CONTROL + 'w') on other OSs.
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w')
driver.close()
I hope this solves your problem.
I want to open a new tab and close the previous tab. But it seems doesn't work! it just open the first link and throws the error: no such window: target window already closed
from selenium import webdriver
driver = webdriver.Chrome()
urls = ['https://google.com','https://facebook.com','https://instagram.com']
for i in range(3):
driver.execute_script("window.open('"+ str(urls[i]) +"');")
driver.close()
Thanks in advance!
After opening a new tab you need to close the current (old) tab and then to switch to the new tab.
The code below works
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
urls = ['https://google.com', 'https://facebook.com', 'https://instagram.com']
for i in range(3):
# open a new tab
driver.execute_script("window.open('" + str(urls[i]) + "');")
# close the current, old tab
driver.close()
# get the handle of the new, recently opened tab
window_name = driver.window_handles[0]
# switch to the recently opened tab
driver.switch_to.window(window_name=window_name)
I am developing a program that opens a web driver. Now I want to open that driver and after opening it should open a new chrome tab with a link. How can I do that?
Please help
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
driver.get(url)
I am using this to open the driver (url= is a variable from above)
after opening want that it opens a new chrome tab please do that!
One way to do that is the following:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
BASE_URL = "https://www.google.com/" # your url
driver = webdriver.Chrome(
executable_path=ChromeDriverManager().install()
)
driver.get(BASE_URL)
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get(BASE_URL)
You spawn a new window, switch to the window and perform a new get.
Here is an example script:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://www.startpage.com/en/')
driver.find_element_by_xpath('//*[#id="query"]').send_keys('Example')
driver.find_element_by_xpath('/html/body/div[1]/main/div[1]/section/div[1]/div[1]/form/button[2]/span[2]').click()
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/section[5]/div[1]/a/h3').click()
At the end, it clicks something that opens a new tab. I want to close the new tab and continue working with the original tab
driver.window_handles is the object for browser tabs.
Assuming that you want to close second tab (new tab), this works.
# continue from your code
driver.switch_to.window(driver.window_handles[1])
driver.close()
Just change i of window_handles[i] for other tab.
To close the new tab and continue working with the original tab you have to:
Switch to the new tab inducing WebDriverWait for number_of_windows_to_be(2) and close().
Switch back to the parent tab.
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://www.startpage.com/en/')
print("Initial Page Title is : %s" %driver.title)
windows_before = driver.current_window_handle
print("First Window Handle is : %s" %windows_before)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.search-form__input"))).send_keys("Example")
driver.find_element_by_css_selector("span.search-form__button-icon").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//h3[#class='w-gl__label']//following::h3[1]"))).click()
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
windows_after = driver.window_handles
new_window = [x for x in windows_after if x != windows_before][0]
driver.switch_to.window(new_window)
print("Page Title after Tab Switching is : %s" %driver.title)
print("Second Window Handle is : %s" %new_window)
driver.close()
driver.switch_to.window(windows_before)
print("Page Title after second Tab Switching is : %s" %driver.title)
print("Current Window Handle is : %s" %windows_before)
Console Output:
Initial Page Title is : Startpage.com - The world's most private search engine
First Window Handle is : CDwindow-18CCC5501A5F68CBE1C3094D0D0B419D
Page Title after Tab Switching is : YouTube
Second Window Handle is : CDwindow-2EDCAB04A232660E8BCBD7A079DE574B
Page Title after second Tab Switching is : Startpage.com Search results
Current Window Handle is : CDwindow-18CCC5501A5F68CBE1C3094D0D0B419D
You can find a relevant detailed discussion in Open web in new tab Selenium + Python
I am working on the below script to open multiple website in new tabs. This is working fine in Firefox as expected.
But it is not working in Chrome. It opens a new tab as the second tab, but the second website link opens in the first tab itself. Later again a new tab is opened as the third tab, then the third link opens in the first tab itself. The commented part ( 5 th line ) is how I call the chromedriver.exe.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox() #Chrome('C:\\data\\books oae\\apex library\\chromedriver') #Firefox()
browser.get('https://trello.com/login')
time.sleep(10)
emailElem = browser.find_element_by_id('user')
emailElem.send_keys('test123#gmail.com')
passwordElem = browser.find_element_by_id('password')
passwordElem.send_keys('test12345')
passwordElem.submit()
body = browser.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')
browser.get('https://todoist.com/Users/showLogin')
time.sleep(10)
emailElem = browser.find_element_by_id('email')
emailElem.send_keys('test123#gmail.com')
passwordElem = browser.find_element_by_id('password')
passwordElem.send_keys('test12345')
passwordElem.submit()
body = browser.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')
browser.get('https://asana.com/#login')
time.sleep(10)
emailElem = browser.find_element_by_id('login-email-login-modal')
emailElem.send_keys('test123#gmail.com')
passwordElem = browser.find_element_by_id('login-password-login-modal')
passwordElem.send_keys('test12345')
passwordElem.submit()
Splinter is the answer to your problem. It's a wrapper around Selenium that does all your Display-handling for you. Just by creating new Splinter objects, you open new windows. You can specify if you want them to be Chrome or Firefox.