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()
Related
How would I make webdriver selenium open another tab, and copy something from that another tab, close it and paste it in the first tab?
How would I do this?
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 30)
Here's an example that opens a page, opens a new tab, copies something, closes the tab, and pastes the text into an input field on the new tab:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://seleniumbase.io/demo_page")
driver.switch_to.new_window("tab")
driver.get("data:text/html,<h1>Page B</h1>")
text = driver.find_element("css selector", "h1").text
driver.close()
window_handle = driver.window_handles[0]
driver.switch_to.window(window_handle)
driver.find_element("css selector", "input").send_keys(text)
driver.quit()
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')
There is a button in the website. When I click by click() method, it opens in the new tab.
I tried this method to access the element which is located in the second tab.
for _ in range(3):
time.sleep(5)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
try:
content = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "/html/body/div/p[1]"))).text
print(content)
except:
print("...")
and the result was,
...
...
...
I guess, it accepts the first tab as an active tab. How to access an element in the other tab, by using python selenium. I searched some solution, and they were saying almost same thing which is to use CTRL+TAB method same as mine...
You need to switch your driver to the new tab, then switch it back when it's done.
Assuming you have 2 tabs:
# you already opened new tab
tab1 = driver.window_handles[0]
tab2 = driver.window_handles[1]
driver.switch_to.window(tab2) # switch to new tab
# do your stuff here
driver.close() # close new tab
driver.switch_to.window(tab1) # switch to original tab
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 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)