It is a memorandum when selenium is installed on Mac and it is used for trial with python.
pip3 install selenium
Install Google Chrome referring to the following
https://www.google.com/chrome How to install Google Chrome on Mac
Check the version of Google Chrome, Install the same version of driver as Google Chrome
Google Chrome menu → About Google Chrome
(Example) Google Chrome is the latest version Version: 81.0.4044.138 (Official Build) (64-bit)
(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:
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
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")
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