Forums

Python Selenium TypeError: init() got an unexpected keyword argument 'options

Hello, I am trying to replicate the following code (tweaking what available here at StackOverflow):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/usr/bin/python3.6

from pyvirtualdisplay import Display
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
[...]

opts = Options()
opts.add_argument('--headless')
driver = Firefox(options=opts)

with Display():
    try:
        driver.get('https://trends.google.it/trends/trendingsearches/realtime?geo=IT&category=s')
[...]

Unfortunately I keep getting the error message

TypeError: init() got an unexpected keyword argument 'options'

What am I doing wrong? Is there something I should be aware of in PythonAnywhere that limitates me?

Thanks in advance!

Not sure you need it but I am running the "battery included" Selenium which is the 2.53.6 for Python 3.6.

Thanks,

P.S. Please remove my other post here: https://www.pythonanywhere.com/forums/topic/27145/

That version of selenium does not have the options argument. You can see the documentation for Firefox object that you're using by starting a Python console and:

from selenium.webdriver import Firefox
help(Firefox)

Thanks Glenn.

AFAIK I can't use another version without using a virtualenv and I can't use Chrome WebDriver as well, right?

(reference here: https://help.pythonanywhere.com/pages/selenium/)

You cannot use a different version at all with Firefox. That's the latest version of selenium that works with the version of Firefox that will run on PythonAnywhere. If you want to use Chrome, we can update you to our new, experimental virtualization system. Then you can install the most recent version of selenium and use it with Chrome.

Hi glenn, and thanks for your help.

I am sorry but I am stuck since I have got another error :( I did what you recommended

You can see the documentation for Firefox object that you're using by starting a Python console and: from selenium.webdriver import Firefox help(Firefox)

And now my code looks like the following one:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/usr/bin/python3.6

from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
[...]

opts = Options()
opts.add_argument('--headless')
driver = Firefox(firefox_options=opts)

with Display():
    try:
        driver.get('https://trends.google.it/trends/trendingsearches/realtime?geo=IT&category=s')
[...]

where firefox_options should be the right argument for my Selenium installation.

But now I get the error:

  File "/usr/lib/python3.6/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 99, in _wait_until_connectable
    "The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the 
FirefoxBinary constructor, check it for details.

It gets raised at the line where my code says:

driver = webdriver.Firefox(firefox_options=opts)

Any kind suggestion? I have already tried to search on PythonAnywhere forums and on the web and the only hint I have is it could be caused by the location of geckodriver or something like that, what do you think?

Many thanks in advance.

take a look at this.

It seems that you are doing driver = Firefox(firefox_options=opts) before you start the display?

You're right, my question was such a dumb one :)

Thanks for the reply! PA Rocks <3

Glad we could help!

TypeError: init() got an unexpected keyword argument 'options'

Hi! I was trying to implement Headless Chrome with the help of https://www.pythonanywhere.com/forums/topic/21948/#id_post_76488

import sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import random
from time import sleep
[...]
class Bot:
def __init__(self, username, password):
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--disable-gpu")
    self.driver = webdriver.Chrome('./chromedriver', options = chrome_options)
[...]

Executing the code always gives me:

self.driver = webdriver.Chrome('./chromedriver', options = chrome_options) TypeError: init() got an unexpected keyword argument 'options'

But the code executes fine on my PC.

Is something wrong with my code? Is Chrome not activated on my account?

Thanks in advance!

Now it is activated for you.

Can I get the chrome activation too por favor

Sure, no problem -- that's done now. The next step is for you to upgrade Selenium for your account -- for example, if you're using Python 3.7, run this in Bash:

pip3.7 install --user --upgrade selenium

...and then you can run Selenium with Chrome using code like this:

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("Page title was '{}'".format(browser.title))

finally:
    browser.quit()

Hi,

Can I get the chrome activation too? Thanks in advance.

No problem. I have enabled it for your account.

Thanks a lot.

Hi,

Can I get the chrome activation too? Thanks in advance. :P

@trabajalia it's done for you.

Can I get the chrome activation too please :)

Sure, no problem -- see my earlier post for the next steps.

can i get the chrome activation too ?

Yes, @mrspock, it's done for you.

Hello,

I am able to use the chrome options within a flask server.py file. Next, I would like to schedule a daily task to automatically search for updates (let's call it update.py). I am using exactly the same code snipped in both files being the following:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
browser = webdriver.Chrome(options=chrome_options)
...
browser.quit()

However, the update.py fails for the following line with a TypeError:

browser = webdriver.Chrome(options=chrome_options)
TypeError: __init__() got an unexpected keyword argument 'options'

Can I use the chrome options only within a flask file?

Thanks for your help! :)

Hi, are you running the task using the same Python executable? If you're using virtualenv in your web app (which is working), you should set up the task to use it too: https://help.pythonanywhere.com/pages/VirtualEnvInScheduledTasks/.

ah, I was missing that part - it worked now! :) Thx for the hint.

Good, thanks for confirming that.

Hello, Can I please get the Chrome activation. Thank you

@WinBigSlots It's enabled for you.

Hi can I also get the Chrome activation? I'm having the same issues.

@michaelturner008 It's done for you

Can I please get the chrome activation on my account?

In order to enable it for your account, we will also need to update your system image. Note that switching to the new system image may break some of your existing code that worked on older versions. You may need to recreate any virtualenvs that you're using and you will need to start new consoles and reload your web apps. You will also need to kill any ipython notebook processes that are running by using the "Fetch processes" button on the Consoles page.

Hi,

Can I get the chrome activation too? Thanks in advance.

No problem. I have enabled it for your account.

Hello! Can I get Chrome in my acc too? Thx in advance

No problem. I have activated it for your account.

Hello! I also need chrome in my account.. thanks

It is already enabled on your account.

Hello! please activate chroma in my account please.. thanks

@chetan1511 It's done for you

Can i also get a chrome activation please. Thank you very much

It's already activated for you; new accounts created after 15 March 2021 have it switched on by default (and older accounts will be changed to have it over the coming weeks). To use it, just follow the instructions in my previous post.

Hi giles, Can i also get a chrome activation please.. Thanx in advance

We're in the process of migrating all existing accounts over to the new virtualization system -- I've made a note to include you in one of the first batches. We'll post back here when it's done.

Can I get a chrome activation, please. Thank you

@pepew you have just been moved over. You will need to pip upgrade selenium to run the latest headless chrome.

Can I also please have chrome activation. Thanks!

Hi @jacomorr -- it's already switched for you. You need to upgrade Selenium, if you haven't done it yet and it should work for you.

Hi can I also get the chrome activation please? I've already upgraded Selenium in bash.

Hi can I also get the chrome activation please? I've already upgraded Selenium in bash.

It is already enabled for your account.

Can you enable Chrome for me too? :-)

hi there, we already have the virtualization system enabled for your account. ie. this means that you will be able to run chrome if you have everything installed etc. However, you currently do not have anything installed. If you want we can switch you over to the latest system image, but keep in mind that that will upgrade python packages and other packages-- which may cause your existing code to break.

Hi, Can I get the chrome activation too? : )

@Diksha5Agarwal It is already enabled on your account.

Hi, Can you activate my chrome as well? Thanks in advance. :)

@RyeGuyRyan -- it's already enabled on your account. If it's not working, check if you followed all the steps suggested here: https://www.pythonanywhere.com/forums/topic/27144/#id_post_84963.

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("Page title was '{}'".format(browser.title)) finally: browser.quit()

Selenium upgraded, but I have message: Traceback (most recent call last): File "/home/Silens82/mysite/flask_app.py", line 5, in <module> browser = webdriver.Chrome(options=chrome_options) TypeError: init() got an unexpected keyword argument 'options'

what I do wrong?

The most likely is that you upgraded selenium in one version of Python and you are running your code in another. pip installs/upgrades into Python 2.7, pip3.6 installs/upgrades into Python 3.6 etc.

Hi guys, would you be so kind and activate Chrome for my account as well please?

hi there, you already have it turned on for you.

Hi, why am i still getting the same error even though i already update to the latest selenium using 'pip3.7 install --user --upgrade selenium' command ? thanks in advance

@swhusky How are you running your code? Are you sure you're using Python 3.7?

thanks a lot! it worked now

No problem, glad to hear that!

Hi, Can I get the chrome activation too? : )

It is already activated for your account. All accounts have it activated by default now.

Hi, I did the update pip3.7 install --user --upgrade selenium in my virtualenvs but I already receive the type error "init() got an unexpected keyword argument 'options'". Is my account is activated to use selenium?

Yes, it is. If you're getting that error, either you have not upgraded selenium or you are using selenium in an environment (python version/virtualenv) where you have not upgraded it.

ok, thanks for the help. My virtualenvs was installed with python 3.8 so I had to upgrade using pip3.8 not pip 3.7. now , its working.

Thanks for confirming -- glad we could help :-)

I was able to get selenium working and wanted to add that it will NOT work with pip3.8 install --user --upgrade selenium. You will get an error when sending keys (AttributeError: 'dict' object has no attribute 'get_attribute') because it is installing the newest selenium with PythonAnywhere's old version of chrome driver.

You will want to install Selenium 3.1 to fix that issue. pip3.8 install --user selenium==3.141.0

Thanks for posting that! You're quite right -- we say that on our help page for Selenium, but it's good to have it mentioned on this forum thread too.

Hello I would like to activate selenium on my pythonanywhere account.

It's already activated on all accounts -- you may want to change your system image to the most recent one, or update selenium, see this help page.

please enable selenium in my account. thanks in advance

@konex It's already activated on all accounts running current system image -- you may want to change your system image to the most recent one, see this help page and that one.