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.