Selenium get session ID of new window - Python - python

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)

Related

Closing tab in selenium is making current_window_handle = NoneType, even when there is another tab open

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')

How To Block Chrome Window Popup in Selenium Python

I have a Selenium-Python script for performing some automation tests on website. The script repeatedly opens some new tabs performs some work on the opened window and closes it.
Issue I'm facing is that whenever a new tab is opened my chrome window pops up from Minimize state to maximize. I want it to do all the task in background without interuppting.
Ps: Headless version is not applicable for my scenario.
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=chrome_options,executable_path="chromedriver.exe")
driver.get("https://xyx.org/#/login") #Login manually to a website.
while 1:
#some stuff here
main_window = driver.current_window_handle
driver.execute_script("window.open();")
driver.switch_to.window(driver.window_handles[1])
driver.get("some link here ")
#doing some work here
driver.close()
driver.switch_to.window(main_window)
If I minimize the chrome window manually then whenever the driver.execute_script("window.open();") is executed it automatically maximizes the chrome window. I want it to just keep remain minimized and do the work.
My fixed solution, using two driver instead of one. Driver one to login and driver two to do the work on the window with headless mode.
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
options = Options()
options.headless = True
driver_one = webdriver.Chrome(executable_path=r'/chromedriver')
driver_one.maximize_window()
driver_one.get("https://xyx.org/#/login") #Login manually to a website.
while 1:
#some stuff here
driver_two = webdriver.Chrome(executable_path=r'/chromedriver',
options=options)
link = "https://www.google.com"
driver_two.get("some link here ")
#doing some work here
driver_two.close()

Selenium Ghost browser unexpected exited status 0

Browser opens and driver loses its control. It starts the browser but it can't initiate the driver in order to use it and send_keys, or do anything.
The code runs using Ghost Browser, which is a chromium based browser.
What should be done in order to selenium get control over browser?
Ive tried to get session_id in order to attach selenium to existing browser but it didnt worked also, since it cant get the session_id, because selenium exits.
Code:
exe_path = r'C:\Users\Anonymous\AppData\Local\GhostBrowser\Application\ghost.exe'
driver = webdriver.Chrome(executable_path=exe_path)
driver = webdriver.Chrome(executable_path=exe_path)
Are you sure you use chrome? Maybe change the webdriver.Chrome

selenium new tab in chrome browser by python webdriver

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)

Send keys control + click in Selenium with Python bindings

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.

Categories

Resources