Installing Selenium and Webdriver for Python
We will use a virtual environment for running Python scripts. Follow the below steps to create Python virtual environment and install the required python modules.
- Create a directory to store Python scripts. Then switch to the newly-created directory.
mkdir tests && cd tests
- Set up the Python virtual environment and activate it.
python3 -m venv venv
source venv/bin/activate
Once the environment is activated, You will find the updated prompt as shown below screenshot:
Now use PIP to install the selenium and webdriver-manager Python modules under the virtual environment.pip install selenium webdriver-manager
Installing Seleniu
Selenium Python Script with Headless Chrome
Now, create a sample selenium script in Python that fetches the title of a website.
This
script will run headless, So you can run it without an X desktop
environment. You can simply SSH to your system and run the below
example. Make sure Chromedriver is install in arm64 type. See my other blog
- Create a Python script and edit it in your favorite text editor:
nano test.py
- Copy-paste the following Selenium Python script to the file.
====
from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Service#from webdriver_manager.chrome import ChromeDriverManagerchrome_driver_path="/usr/bin/chromedriver"options = Options()options.add_argument('--headless')options.add_argument('--no-sandbox')options.add_argument('--disable-dev-shm-usage')#driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)driver= webdriver.Chrome(executable_path=chrome_driver_path, options=options)driver.get("https://python.org")print(driver.title)driver.close()====
Press
CTRL + O
to save content to file and pressCTRL + X
to close editor. - Now, run this Python script in a shell.
python test.py
You will see the output something like below:
No comments:
Post a Comment