Install selenium on Mac and try it with python

Purpose

It is a memorandum when selenium is installed on Mac and it is used for trial with python.

Install Selenium / Google Chrome / Google Chrome driver

Install Selenium

pip3 install selenium

Install Google Chrome

Install Google Chrome referring to the following

https://www.google.com/chrome How to install Google Chrome on Mac

Install Google Chrome driver

Check the version of Google Chrome, Install the same version of driver as Google Chrome

Check Google Chrome version

Google Chrome menu → About Google Chrome

(Example) Google Chrome is the latest version Version: 81.0.4044.138 (Official Build) (64-bit)

Install the same version of driver as Google Chrome

(Example) Google Chrome version: 81.0.4044.In the case of 138
$ pip3 install chromedriver-binary==81.0.4044.138
Successfully installed chromedriver-binary-81.0.4044.138.0
$ pip3 show chromedriver-binary
Name: chromedriver-binary
Version: 81.0.4044.138.0
Summary: Installer for chromedriver.
Home-page: https://github.com/danielkaiser/python-chromedriver-binary
Author: Daniel Kaiser
Author-email: [email protected]
License: MIT
Location: /anaconda3/lib/python3.7/site-packages
Requires: 
Required-by: 

Run

Refer to the following and execute the test program "Start Chrome, search Google for" Selenium ", and open the official Selenium website."

Preparation notes for running Selenium ChromeDriver & Python on Mac

selenium-test.py


# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import chromedriver_binary

#Open your browser.
driver = webdriver.Chrome()
#Open the Google search TOP screen.
driver.get("https://www.google.co.jp/")

#Type "selenium" as the search term and press Enter.
search = driver.find_element_by_name('q') 
search.send_keys("selenium automation")
search.send_keys(Keys.ENTER)
#The title is "Selenium-Click the link that matches "Web Browser Automation".
#element = driver.find_element_by_partial_link_text("SeleniumHQ Browser Automation")
#element = driver.find_element_by_link_text("WebDriver")
element = driver.find_element_by_partial_link_text("Selenium")
element.click()

#Wait for 5 seconds.
sleep(5)
#Quit the browser.
driver.close()

Execute

$ python3 selenium-test.py

It is OK if the browser closes after 5 seconds after automatically transitioning from google to the selenium page

スクリーンショット 2020-05-17 17.45.46.png

FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

$ python3 selenium-test.py
Traceback (most recent call last):
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/anaconda3/lib/python3.7/subprocess.py", line 769, in __init__
    restore_signals, start_new_session)
  File "/anaconda3/lib/python3.7/subprocess.py", line 1516, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "selenium-test.py", line 7, in <module>
    driver = webdriver.Chrome()
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

I installed the Chrome driver while checking the version, but it was not used correctly. Import and solve

import chromedriver_binary

Once installed, you may want to add /usr/local/lib/python3.7/site-packages/chromedriver_binary/ to your PATH or import chromedriver_binary at the beginning of your Python script.

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="lst-ib"]"}

$ python3 selenium-test.py
Traceback (most recent call last):
  File "selenium-test.py", line 12, in <module>
    driver.find_element_by_id("lst-ib").send_keys("selenium")
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="lst-ib"]"}
  (Session info: chrome=81.0.4044.138)

It seems that I could not find the search window below, so

driver.find_element_by_id("lst-ib").send_keys("selenium")

Replace with the following to solve

search = driver.find_element_by_name('q')   

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"SeleniumHQ Browser Automation"}

Traceback (most recent call last):
  File "selenium-test.py", line 18, in <module>
    element = driver.find_element_by_link_text("SeleniumHQ Browser Automation")
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 428, in find_element_by_link_text
    return self.find_element(by=By.LINK_TEXT, value=link_text)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"SeleniumHQ Browser Automation"}
  (Session info: chrome=81.0.4044.138)

It seems that I could not search for "Selenium HQ Browser Automation" in the following part, so

element = driver.find_element_by_partial_link_text("SeleniumHQ Browser Automation")

This time, find_element_by_partial_link_text ("Selenium") is used for partial search to solve the problem.

element = driver.find_element_by_partial_link_text("Selenium")

reference

Preparation memo to run Selenium ChromeDriver & Python on Mac Selenium automates browsers. That's it! How to install Google Chrome on Mac Prepare a Python development environment! (Mac) What to do if selenium displays the error "Message: session not created: This version of ChromeDriver only supports Chrome version 75" selenium Downloads No such file or directory:'chromedriver':'chromedriver' resolution selenium does not start Preparation memo to run Selenium ChromeDriver & Python on Mac Summary of how to select elements in Selenium [Python] find_element_by_link_text ・ ・ ・ Get element from linkText https://www.google.com/chrome

Recommended Posts

Install selenium on Mac and try it with python
Install lp_solve on Mac OS X and call it with python.
Install Python on Mac
Install Python 3 on Mac
Install Python 3.4 on Mac
Try running Google Chrome with Python and Selenium
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Try working with Mongo in Python on Mac
Try importing MLB data on Mac and Python
Install pygame on python3.4 on mac
Install pandas 0.14 on python3.4 [on Mac]
Install Python3 on Mac and build environment [Definitive Edition]
Automatic follow on Twitter with python and selenium! (RPA)
Automate Chrome with Python and Selenium on your Chromebook
Install Python 2.7.9 and Python 3.4.x with pip.
Scraping with Python, Selenium and Chromedriver
Install Python 3.7 Anaconda on MAC, but Python 2
Install python3 on Mac (El Capitan)
Install fabric on Ubuntu and try
Steps to install python3 on mac
Install python with mac vs code
Install CaboCha in Ubuntu environment and call it with Python.
How to install OpenCV on Cloud9 and run it in Python
Install mecab on Sakura shared server and call it from python
Build a Python environment on your Mac with Anaconda and PyCharm
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
A memo with Python2.7 and Python3 on CentOS
Follow active applications on Mac with Python
Quickly install OpenCV 2.4 (+ python) on OS X and try the sample
scipy stumbles with pip install on python 2.7.8
Notes on building Python and pyenv on Mac
Build Python environment with Anaconda on Mac
python on mac
Practice web scraping with Python and Selenium
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Deploy a Python app on Google App Engine and integrate it with GitHub
Install ROS and ROS module for Roomba on RaspberryPi3 and try to run it
Try to build python and anaconda environment on Mac (by pyenv, conda)
Put MeCab binding for Python with pip on Windows, mac and Linux
Send and receive binary data via serial communication with python3 (on mac)
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Install and run Python3.5 + NumPy + SciPy on Windows 10
Try it with Word Cloud Japanese Python JupyterLab.
[Ansible] Install dnf on Centos7 with Python3 interpreter
Drag and drop local files with Selenium (Python)
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
Install Python 3 on MacOS Catalina (with Homebrew only)
Install MongoDB on Ubuntu 16.04 and operate via python
Install Python and libraries for Python on MacOS Catalina
Install ZIP version Python and pip on Windows 10
Install Tensorflow on Mac
Install python on WSL
Try scraping with Python.
Install pyenv on mac
Install Python on Pidora.
Install Scrapy on python3
Install Ansible on Mac
Install Python3.4 on CentOS 6.6
Install Voluptuous with Python 2.5
ScreenShot with Selenium (Python)