How to close the browser and launch the browser again using selenium with python? Can you give sample code for this? - selenium

The below code throwing error.it's suceessfuflly closed but unable to launch the browser again.
driver.close()
driver.get("https://google.com/")

driver.close() closes the browser window which is currently in focus. If there is more than one window opened, then driver.close() will close only the current active window, while the remaining windows will not be closed.
So if you have other windows opened, before loading a new page you first have to switch to one of them using
idx = ... # index of one of your windows
driver.switch_to.window( driver.window_handles[idx] )
If there aren't other windows opened, then you have to quit the driver and start a new one.
driver.quit()
driver = webdriver.Chrome(...)

driver.close()
driver.close() closes the current top-level browsing context. If there are no more open top-level browsing contexts, then it closes the session.
As driver.close() closes only the current top-level browsing context, if there are more top-level browsing context then you need to switch tab to any other top-level browsing context.
Else if there are no more top-level browsing context then driver.close() closes the session and you have to reinitialize the WebDriver and Browser client. This is equivalent to invoking driver.quit()
Java snippet:
driver.close()
WebDriver driver = new ChromeDriver();
Python snippet:
driver.close()
driver = webdriver.Chrome()

Related

Chrome Webdriver detach option in Java

I am using ChromeDriver for Selenium in Java.
I want to leave the browser open after the test is complete. The default behavior is to close the browser.
I have tried the following
options.setCapability("detach", true);
driver = new ChromeDriver(options);
or
options.setExperimentalOption("detach", true);
Neither seems to work. What is the correct way to use this in Java?
According to detach the property description
If false, Chrome will be quit when ChromeDriver is killed, regardless of whether the session is quit. If true, Chrome will only be quit if the session is quit (or closed). Note, if true, and the session is not quit, ChromeDriver cannot clean up the temporary user data directory that the running Chrome instance is using.
which means (AFAIU) that this only controls if you want to keep your browser open or not in case when your WebDriver process is unexpectedly terminated. This does not cover your particular case.
You can just leave your driver active after the test has completed (not quit it) and create new WebDriver object for each new test. However this is not effective approach from performance standpoint.

How can we place the cursor in Chrome Driver address bar and perform Key.Enter events using selenium Web driver using java?

I am automating the application using selenium which is enabled with SSO(VIDM) integration which will take my system domain name and password. The moment I hit the application URL, It will process the SSO(VIDM) and displaying the browser pop up with SSL certificate , OK button. I am unable to locate "OK" button as inspect is blocked for that pop up.
Manual Work Around noticed: Able to select that certificate from the pop up with out clicking okay button by placing the cursor in Chrome driver address bar and then perform enter using the system keyboard.Please see the image here
Add the below chromedriver option to launch specific profile.
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
ChromeDriver driver = new ChromeDriver(options);
Refer to chromedriver page for more information.
ChromeDriver Capabilities

Selenium with headless chrome fails to get url when switching tabs

I'm currently running Selenium with Specflow.
One of my tests clicks on a button which triggers the download of a pdf file.
That file is automatically opened in a new tab where the test then grabs the url and downloads the referenced file directly to the selenium project.
This whole process works perfectly when chrome driver is run normally but fails on a headless browser with the following error:
The HTTP request to the remote WebDriver server for URL http://localhost:59658/session/c72cd9679ae5f713a6c857b80c3515e4/url timed out
after 60 seconds. -> The request was aborted: The operation has timed out.
This error occurs when attempting to run driver.Url
driver.Url calls work elsewhere in the code. It only fails after the headless browser switches tabs. (Yes, I am switching windows using the driver)
For reference, I cannot get this url without clicking the button on the first page and switching tabs as the url is auto-generated after the button is clicked.
I believe you are just using argument as "--headless" for better performance you should select screen size too. Sometimes, due to inappropriate screen size it cannot detect functions which you are looking for. try using this code or just add one line for size.
from selenium import webdriver
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(chrome_options=chrome_options)
Don't forget to put other arguments according to your need in "driver".

selenium driver close when the new window pop out

I run the test
First I login and the window close and open a new one.
but the test failed.
it show Unable to get browser (NoSuchDriver).
Is it means my driver is been closed?
How to solve it?

Chrome browser opens and closes using watir webdriver

I have the following code:
require "rubygems"
require "watir-webdriver"
browser = Watir::Browser.new(:chrome)
browser.goto "http://google.com"
When i run this code, the browser opens the site google.com and then closes automatically
Why does it close automatically? My chrome version is 12.0.742.68
The Chrome driver will close all browser instances when your program exits. It's a known issue, and its priority was fortunately bumped today - look for a fix soon.