Google chrome closes automatically after launching using Selenium Webdriver - selenium

I am on Windows 10 using Selenium with Python 3.7.3.
If I wrap the code inside a class , the browser terminates immediately after opening the page:
'''
Program to show how to open chrome browser using selenium webdriver
'''
from selenium import webdriver
#import os
class run_chrome_tests(object):
def test_method(self):
# This is the location of the chrome driver saved into a variable
#driver_location = "D:\\Udemy_Python\\Libs\\chromedriver.exe"
# Letting the system environment know the location of the chrome driver
#os.environ["webdriver.chrome.driver"] = driver_location
# Letting the chrome browser know the location of the chrome driver
driver = webdriver.Chrome()
driver.get("http://www.letskodeit.com")
ch = run_chrome_tests()
ch.test_method()

Check the Version of the Chrome Browser and the Version of the chrome Driver , if the driver is not compatible then browser terminates just after opening,try using the latest version for both

By default, the chrome window will shutdown automatically, after it finishes all the instruments, unless you set the option to prevent this explicitly.
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)
driver.get("http://www.bing.com")
However, there is another possibility that you had chosen a WRONG version of chrome driver. The following is on how to select the right version of the chrome driver:
In the address bar of your chrome browser, type chrome://version. The first line of the returned page will be the version number.
Download one version of the chrome driver from this page. (https://chromedriver.storage.googleapis.com/index.html). Select the version that matches the version of your chrome as much as possible. (Very Important)

Related

How to use Headless Chrome Selenium on PythonAnywhere?

"Using Selenium on PythonAnywhere" says:
Firefox only, selenium 2, geckodriver not required (…)
That (Firefox v17.0) is quite an old version, but it
works for most sites.
In my case it did not work. Is there a way to use Google Chrome (headless) anyway on PythonAnywhere?
I found this forum entry with the hint that it is not only possible to use Chrome, but even "You'll need to upgrade Selenium to the most recent version".
I simply wrote an email to the PythonAnywhere support and they enabled that for my (payed) account within hours.
Don't forget to start a new console or restart your web app! Then the following code should work:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
browser = webdriver.Chrome(options=chrome_options)
try:
browser.get("https://www.google.com")
print(f'Page title was {browser.title}')
finally:
browser.quit()

Detecting angularJS elements in IE11 browser using selenium takes long time. How to resolve this?

I'm running selenium tests on IE 11 in angularJS application. It takes more than usual time to detect/select/validate elements while it works better in chrome.
Browser information : Internet explorer 11
Webdriver : IEdriverserver 32bit
selenium version: 3.14
You could try the following methods to make it faster when using selenium IE WebDriver:
Download the recommended 32 bit Windows IE WebDriver version 3.150.1
and latest stable version 3.141.59 of Selenium from this page.
Change your IE settings: Tools -> Internet Options -> Security, set Enable Protected Mode to the same value in all zones (all checked or all unchecked).
Instantiate your InterExplorerWeDriver class with the path of your IE WebDriver like this:
InternetExplorerDriver ieDiver = new InternetExplorerDriver(“Path to the 32 bit IEdriver”);
Add capability nativeEvents as false. Add capability requireWindowFocus as true.
Reference link:
(1) Very slow text entry on IEDriverServer
(2) Selenium WebDriver typing very slow in text field on IE browser

Selenium with firefox addons?

Just wondering, is there anyway to use selenium with firefox addons? I installed an addon but whenever i start the selenium driver it seems to kick off firefox without it. just curious
it simple like this
profile = webdriver.FirefoxProfile()
profile.add_extension("/path/to/file.xpi")
driver = webdriver.Firefox(profile)

Do headless web browser need selenium WebDriver?

I am trying to use headless web browser(such as headless chrome) for our selenium tests.Should I have to use selenium WebDriver(for python or c# bindings)?
Headless Chrome
As per Getting Started with Headless Chrome the Headless Chrome is the server environment where you don't need a visible UI shell.
If you've got Chrome 59+ installed, you start Chrome with the --headless flag as follows:
chrome \
--headless \ # Runs Chrome in headless mode.
--disable-gpu \ # Temporarily needed if running on Windows.
chrome should always point to your installation of Chrome. The exact location of-coarse varies from platform to platform.
ChromeDriver
As per ChromeDriver - WebDriver for Chrome, in simple words WebDriver is an open source tool for automated testing of webapps across many browsers which provides capabilities for navigating to web pages, user input, JavaScript execution, and much more. ChromeDriver is the standalone server which implements WebDriver's wire protocol for Chromium.
Conclusion
If you intend to use Chrome Browser in Headless Mode(i.e. Headless Chrome) for your selenium tests you have to mandatorily use ChromeDriver
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
Before we set up a Chrome web driver instance, we have to create an Options object that allows us to specify how exactly we want to launch Chrome. Let’s tell it that we want the browser to launch headless and that the window size should be set to 1920x1080. We also need ChromeDriver to be able to run Chrome at all 
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
# download the chrome driver from https://sites.google.com/a/chromium.org/chromedriver/downloads and put it in the
# current directory
chrome_driver = os.getcwd() +"\\chromedriver.exe"
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
driver.get("https://www.google.com")

Selenium grid to remote webdriver(chrome) hangs on get

I have a selenium grid with nodes on virtual machine. I can connect, open browser and close it but when i try navigating to a page it hangs on Executing: [get: http://google.com]
I use the latest verions of selenium, chromedriver and java.
Declaration:
DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
driver = new RemoteWebDriver(new Uri(#"http://xxx.xx.xx.xxx:6000/wd/hub"), capabilities);
then i try to use it when i click a button on my win form:
driver.Navigate().GoToUrl("http://www.google.com");
I can see the node gets the command and logs Executing:[get:htttp://www.google.com] but just hangs. Url doesn't change in browser(its "data:," all the time )
I use windows 7 x64 and try to control browser on windows 7 x32. (if i connect to node on my pc the code works fine)
Any ideas?
Thanks!
I am seeing the exact issue with Safari (python). I am using selenium standalone server v2.47.1. My current workaround is to use JS:
"driver.execute_script("window.location.href = '{0}';".format(url))"
I also observed that if you don't set a homepage on Safari or have it default to load the homepage on new tab/window, the webdriver acts up and hangs on 'get' method.