Search code examples
pythonseleniumselenium-webdriverpython-2.6centos6.8

SyntaxError: invalid syntax with python2 and selenium in centos6.8


I run python script:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
# from pyvirtualdisplay import Display

profile = Options()
profile.headless = True
profile.add_argument('-width=800')
profile.add_argument('-height=600')

# display = Display(visible=0, size=(800, 600))
# display.start()

driver = webdriver.Firefox(options=profile, executable_path='./geckodriver')

driver.get('http://192.168.1.3')
elem = driver.find_element_by_id('sitename').text
print(elem)
driver.quit()

I try run script with pyvirtualdisplay, but error is still here.

Traceback (most recent call last):
  File "script.py", line 4, in <module>
    from selenium import webdriver
  File "/opt/pyscript/venv/env_selenium/lib/python2.6/site-packages/selenium/webdriver/__init__.py", line 18, in <module>
    from .firefox.webdriver import WebDriver as Firefox  # noqa
  File "/opt/pyscript/venv/env_selenium/lib/python2.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 29, in <module>
    from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
  File "/opt/pyscript/venv/env_selenium/lib/python2.6/site-packages/selenium/webdriver/remote/webdriver.py", line 1115
    return {k: size[k] for k in ('width', 'height')}
                         ^
SyntaxError: invalid syntax

I install also fontconfig freetype freetype-devel fontconfig-devel libstdc++ in CentOS. And I try run with browser phantomjs, this problem still here. Version: CentOS 6.8, python 2.6.6, selenium PyVirtualDisplay 0.2.5, selenium 3.141.0 How can I solve this problem?


Solution

  • There is an incompatibility between Selenium and Python2.6 around dictionary comprehensions, which is what this line is.

    return {k: size[k] for k in ('width', 'height')}
    

    The issue has been reported a while ago and closed here. Also, Python 2 has reached end of life, so without support to fix issues like the one you are facing, this should be a strong incentive to upgrade to Python 3.x.

    The not ideal alternative - downgrade selenium.