I am not being able to open a new tab in chrome. My requirement is to open a new tab do some operation then close this new tab and come back to old tab.
The below python code worked in Firefox but not working in Chrome. Could anyone please help me?
ActionChains(driver).key_down(Keys.CONTROL,body).send_keys('t').key_up(Keys.CONTROL).perform()
Guess this will be helpful:
from selenium import webdriver
driver = webdriver.Chrome()
driver.execute_script("window.open('','_blank');")
This piece of code should start new Chrome browser session and open blank page in new tab
Use driver.execute_script("window.open('URL');") to open new tab with required URL
I failed to open new tab with required URL by driver.execute_script("window.open('URL');").
Therefore I changed my mind.
Any link will start on the new tab if we consider switching the current window to the new one. And I will open the new tab by driver.get(URL). The only method I need to use is driver.switch_to_window(driver.window_handles[1]).
We simply switch the window to the main window, when we close the new tab:driver.switch_to_window(driver.window_handles[0]) or driver.switch_to_window(main_window)
By the way, it will raise an error if we don't switch to the main window after closing the new tab.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.google.com/")
# save main_window
main_window = driver.current_window_handle
# obtain url of gmail on the home page of Google
addr = driver.find_element_by_xpath('//*[#id="gbw"]/div/div/div[1]/div[1]/a').get_attribute("href")
# open new blank tab
driver.execute_script("window.open();")
# switch to the new window which is second in window_handles array
driver.switch_to_window(driver.window_handles[1])
# open successfully and close
driver.get(addr)
driver.close()
# back to the main window
driver.switch_to_window(main_window)
driver.get(addr)
Related
As the title says, I'm having an issue where I have two tabs opened, trying to close all but 1 of them, and as soon as I close a tab, the current_window_handle value gets deleted. However, I do notice there is still my window handle in the window_handles list. Wondering if anyone else has encountered this issue as well.
The code in question:
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://example.com')
browser.execute_script("window.open('');")
window_id = browser.window_handles[-1]
browser.switch_to.window(window_id)
browser.close()
browser.get('https://google.ca')
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
selenium==3.141.0
chrome=93.0.4577.63
ChromeDriver=93.0.4577.63
Fixed the issue. Just add
window_id = browser.window_handles[0]
browser.switch_to.window(window_id)
to reset the current_window_handle property.
So now the code is
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://example.com')
browser.execute_script("window.open('');")
window_id = browser.window_handles[-1]
browser.switch_to.window(window_id)
browser.close()
window_id = browser.window_handles[0]
browser.switch_to.window(window_id)
browser.get('https://google.ca')
This question already has answers here:
Selenium Switch Tabs
(4 answers)
Closed 4 years ago.
I am new at Python and i was just trying to write a code to open new tabs in the browser.So i searched for some ways to do that and i came across this process.But though it works nicely in the youtube videos mine simply doesnt work.It only just opens a new window and goes to google.com.But it does not press the ctr+t to open a new tab. I dont know why cause it doesnt even show any error in the python shell. Was hoping someone could help me with this and tell me what is wrong with my code.Thank you
#! python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser=webdriver.Firefox()
browser.get('http://www.google.com')
elm=browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t')
time.sleep(2)
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL+Keys.PAGE_DOWN)
Here's a code snipped to open a new tab in selenium:
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from time import sleep
browser = webdriver.Firefox()
browser.get('https://www.google.com?q=python#q=python')
first_result = ui.WebDriverWait(browser, 15).until(lambda browser: browser.find_element_by_class_name('rc'))
first_link = first_result.find_element_by_tag_name('a')
# Save the window opener (current window, do not mistaken with tab... not the same)
main_window = browser.current_window_handle
# Open the link in a new tab by sending key strokes on the element
# Use: Keys.CONTROL + Keys.SHIFT + Keys.RETURN to open tab on top of the stack
first_link.send_keys(Keys.CONTROL + Keys.RETURN)
# Switch tab to the new tab, which we will assume is the next one on the right
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
# Put focus on current window which will, in fact, put focus on the current visible tab
browser.switch_to_window(main_window)
# do whatever you have to do on this page, we will just got to sleep for now
sleep(2)
# Close current tab
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')
# Put focus on current window which will be the window opener
browser.switch_to_window(main_window)
I've been searching around for answers, but I cannot get my code to work. What I would like to happen is that I can open a tab, open a new webpage, and then close that same tab. From there, once i'm done doing my work, I'd like it to quit the program. As of now, it will not open a new tab, close the new tab, and quit the application. What is doing right now, is that it opens up google, then loads stackoverflow in the same tab.
Right now, I have...
driver = webdriver.Firefox()
driver.get("https://www.google.com")
time.sleep(5)
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
time.sleep(5)
driver.get("https://www.stackoverflow.com")
time.sleep(5)
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w')
time.sleep(5)
driver.Dispose()
Does anyone have any ideas?
Thanks
Try below code to get desired result:
driver = webdriver.Firefox()
driver.get("https://www.google.com")
# Get current window/tab ID
current_window = driver.current_window_handle
# Open StackOverflow in new window/tab
driver.execute_script("window.open('https://www.stackoverflow.com')")
# Get new window/tab ID
new_window = [window for window in driver.window_handles if window != current_window][0]
# Switch to new window/tab
driver.switch_to.window(new_window)
# Close new window/tab
driver.close()
# Switch to initial window/tab
driver.switch_to.window(current_window)
# Quit Webdriver
driver.quit()
I am using Selenium in order to perform some checks on Chrome.
I'm automatically browsing to "chrome://flags/#enable-quic" and there in the drop down I (automatically) choose "Enable".
As written below, one has to relaunch in order for the changes to take effect.
I want to open a new tab on the new re-launched window to do some more stuff.
A snip of the code:
browser = webdriver.Chrome()
browser.get("chrome://flags/#enable-quic")
browser.find_element_by_xpath("//*[#id='enable-quic']/table/tbody/tr/td/div[1]/div/div[2]/select/option[2]").click() #Select "Enable"
time.sleep(5)
browser.find_element_by_xpath("//*[#id='flagsTemplate']/div[5]/button").click() #Click relaunch
time.sleep(5)
browser.execute_script("window.open('https://www.gmail.com');") #Exception after this line
The exception I get is:
selenium.common.exceptions.NoSuchWindowException: Message: no such window: window was already closed
Someone has an idea how to handle this?
Thanks
After clicking "Refresh" button you get new Chrome window. You should try to switch to that window before executing JavaScript:
browser = webdriver.Chrome()
browser.get("chrome://flags/#enable-quic")
browser.find_element_by_xpath("//div[#id='enable-quic']//select[#class='experiment-select']/option[2]").click()
time.sleep(5)
browser.find_element_by_xpath("//button[#class='experiment-restart-button']").click()
time.sleep(5)
browser.switch_to.window(browser.window_handles[0])
browser.execute_script("window.open('https://www.gmail.com');")
re-launching Chrome is a bad idea because Chromedriver has a reference to the original chrome process.
no such window: window was already closed
so... browser is still pointing to the old window.
Instead of trying to relaunch Chrome, just set the option when you create a Chromedriver instance.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--enable-quic')
chrome = webdriver.Chrome(chrome_options=chrome_options)
I need to open link in new tab using Selenium.
So is it possible to perform ctrl+click on element in Selenium to open it in new tab?
Use an ActionChain with key_down to press the control key, and key_up to release it:
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('http://google.com')
element = driver.find_element_by_link_text('About')
ActionChains(driver) \
.key_down(Keys.CONTROL) \
.click(element) \
.key_up(Keys.CONTROL) \
.perform()
time.sleep(10) # Pause to allow you to inspect the browser.
driver.quit()
Two possible solutions:
opening a new tab
self.driver = webdriver.Firefox()
self.driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
this is the solution for MAC OSX. In other cases you can use the standard Keys.CONTROL + 't'
opening a new webdriver
driver = webdriver.Firefox() #1st window
second_driver = webdriver.Firefox() #2nd windows
Below is what i have tried for Selenium WebDriver with Java binding and its working for me.
If you want to manually open the Link in New Tab you can achieve this by performing Context Click on the Link and selecting 'Open in new Tab' option. Below is the implementation in Selenium web-driver with Java binding.
Actions newTab= new Actions(driver);
WebElement link = driver.findElement(By.xpath("//xpath of the element"));
//Open the link in new window
newTab.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
Web-driver handles new tab in the same way to that of new window. You will have to switch to new open tab by its window name.
driver.switchTo().window(windowName);
You can keep track of window-names which will help you to easily navigate between tabs.
By importing Keys Class, we can open page in new tab or new window with CONTROL or SHIFT and ENTER these keys:
driver.find_element_by_xpath('//input[#name="login"]').send_keys(Keys.CONTROL,Keys.ENTER)
or
driver.find_element_by_xpath('//input[#name="login"]').send_keys(Keys.SHIFT,Keys.ENTER)
For python, a good solution would be driver.find_element_by_css_selector('<enter css selector here>').send_keys(Keys.CONTROL, Keys.RETURN)
After that, you should change windows so selenium could function in the new window
window_before = driver.window_handles[0]
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)
After using the window is useful to close it or go back to the original:
Close tab: driver.close()
Back to the original: driver.switch_to_window(window_before)
Also try driver.switch_to.window() if your Selenium version does not support the other one
Following is working for me to open link in new tab :
String link = Keys.chord(Keys.CONTROL,Keys.ENTER);
driver.findElement(By.linkText("yourlinktext")).sendKeys(link);
Above code is in java. you can convert to python easily I assume.
Please ask if have any query.