Run Python On Ubuntu
Run Python On Ubuntu
Most factory versions of Ubuntu 18.04 or Ubuntu 20.04 come with Python pre-
installed. Check your version of Python by entering the following:
python ��version
If the revision level is lower than 3.7.x, or if Python is not installed, continue
to the next step.
Step 1: Update and Refresh Repository Lists
Open the terminal by searching for it in the dashboard or pressing Ctrl + Alt + T
and enter the following:
python ��version
Check Python version to confirm installation.
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------------
To use the WebDriver API in Python, you must first install the Selenium Python
bindings. This will give you access to your browser from Python code. The easiest
way to install the bindings is via pip.
On Ubuntu/Debian systems, this will install pip (and dependencies) and then install
the Selenium Python bindings from PyPI:
#!/usr/bin/env python
Here is a simple functional test in Python, using Selenium WebDriver and the
unittest framework:
#!/usr/bin/env python
import unittest
from selenium import webdriver
class TestUbuntuHomepage(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
def testTitle(self):
self.browser.get('http://www.ubuntu.com/')
self.assertIn('Ubuntu', self.browser.title)
def tearDown(self):
self.browser.quit()
if __name__ == '__main__':
unittest.main(verbosity=2)
Output:
testTitle (__main__.TestUbuntuHomepage) ... ok
----------------------------------------------------------------------
Ran 1 test in 5.931s
OK
-----
Install latest google-chrome webdriver for Python-selenium binding:
$ LATEST=$(wget -q -O - http://chromedriver.storage.googleapis.com/LATEST_RELEASE)
$ wget http://chromedriver.storage.googleapis.com/$LATEST/chromedriver_linux64.zip
$ unzip chromedriver_linux64.zip && sudo ln -s $PWD/chromedriver
/usr/local/bin/chromedriver
Try below Example to open 'http://www.ubuntu.com/' in google-chrome browser:
#!/usr/bin/env python
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://www.ubuntu.com/')
You may also need to update the path, as explained here
On Unix systems you can do the following to append it to your system�s search path,
if you�re using a bash-compatible shell:
export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
On Windows you will need to update the Path system variable to add the full
directory path to the executable geckodriver manually or command line(don't forget
to restart your system after adding executable geckodriver into system PATH to take
effect). The principle is the same as on Unix.
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
Running a Script :
Once the script has been written, save it to a specific location in your system and
then follow the steps below to run it:
Open the terminal by searching for it in the dashboard or pressing Ctrl + Alt + T.
Navigate the terminal to the directory where the script is located using the cd
command.