How to use Headless Chrome Selenium on PythonAnywhere? - selenium

"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()

Related

New error discovered when using Chromedriver Selenium

When I try to open Chrome with selenium with webdriver, it shows a chrome that looks like this.
Even though it's been a long wait, it's still the same. While I open it by right clicking, it works fine.
I've tried using all versions of Chromedriver 100 and 99, but the results haven't changed much.
Here is the source code that I use:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f"--user-data-dir=G:\\Multi_Chrome\\testing\\Data\\profile")
driver = webdriver.Chrome(service=Service("D:\\Python\\Project\\chromedriver.exe"),
options=chrome_options)
input("Press any key to coutinue ...")
driver.close()
driver.quit()
Any ideas? By the way, I want to ask more, how to open a normal portable chrome. Instead of right clicking and choosing open. Is there a way to automatically open it?
I have partially mentioned it in this article: Open a complete portable chrome like regular Chrome
your question isn't clear because you didn't mention what is the error, but as I see in the screenshot you have already opened one chrome browser before you run the script, but you use --user-data-dir=G:\\Multi_Chrome\\testing\\Data\\profile it means that this profile is already is in use so you cant open another chrome browser with the same profile. so The simplest and easiest fix is to close all running chrome browsers and after that try to run your script again

Google chrome closes automatically after launching using Selenium Webdriver

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)

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)

Running chrome via selenium

I did it earlier but I can't use chrome via selenium now. Browser opens for a few seconds then closes and then I got an error (about 5 minutes later):
Message: session not created
from disconnected: unable to connect to renderer
(Session info: chrome=70.0.3538.67)
(Driver info: chromedriver=2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d),platform=Linux 4.18.14-arch1-1-ARCH x86_64)
I use following code for running browser (which I always use):
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
opts = Options()
browser = Chrome(options=opts)
Chromedriver directory is in the PATH. Versions of chrome and chromedriver you can see in the error. Python 3.7.0, selenium==3.14.0. What's wrong in my actions?
P.S. BTW, It works fine with Firefox
Your versions look compatible with each other based on the compatibility list, so I don't think it has to do with that. I have not seen those options used in that way before though.
Please try this:
from selenium import webdriver
ChromeOptions = webdriver.ChromeOptions()
browser = webdriver.Chrome(chrome_options=ChromeOptions)
browser.get("https://www.google.com")
browser.quit()
Let me know if that is able to open your browser. If it is, then I am assuming you are having issues with some of the options you are passing chrome.
If you are still have issues after checking all the options you are passing chrome, try rolling back your chromedriver version HERE to 2.42. It should still be compatible with chromer version 70.-.
I am on the same versions as you, and I'm not experiencing this issue.
A couple other things to think about:
Are you using headless chrome? If so switch to non-headless and test.
Make sure to close out of all instances of chromedriver before updating with another version.
If chrome recently updated, or you recently updated your driver, try
restarting the machine.
Actually I don't know why, but it works fine now. Everything I did are recommendations from the answer above. It didn't work right after my actions but now it's okay

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