In a survey to write Previously posted Selenium article, I raised Selenium on my private PC from 3 to 4, but along with that, a large amount of Selenium 3 logs that were regularly executed by cron Warning now appears.
Since it is a Warning, the code itself ends normally, but I do not want the log to be unnecessarily long, so I analyzed the source code of Selenium 4 and corrected my code and succeeded in erasing all Warnings, so I will note here I will leave.
DeprecationWarning: executable_path has been deprecated, please pass in a Service object In Selenium3, if the browser driver is not in the PATH, the driver was started as follows.
from selenium import webdriver
driver = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver")
driver.get('https://www.google.com/')
However, if you want to do the same with Selenium 4, you need to pass the executable_path
to the Service
object and then the Service
object as shown below, instead of passing the executable_path
directly when starting the driver.
from selenium import webdriver
from selenium.webdriver.firefox import service as fs
firefox_servie = fs.Service(executable_path="/usr/local/bin/geckodriver")
driver = webdriver.Firefox(service=firefox_servie)
driver.get('https://www.google.com/')
DeprecationWarning: service_log_path has been deprecated, please pass in a Service object
When specifying the log output destination, the specification is to pass service_log_path
to the Service
object as well as executable_path
. However, please be aware of the following two points.
** ① The name of the keyword will be log_path
instead of service_log_path
(2) It is necessary to pass executable_path
to the Service
object even if the browser driver is in the PATH. ** **
So in this case, the 4th line above is as follows.
firefox_servie = fs.Service(executable_path="/usr/local/bin/geckodriver", log_path='/my/log/path')
UserWarning: find_element_by_* commands are deprecated. Please use find_element() instead
As you can see in the message, the function named find_element_by_ *
is now deprecated, and the by_ *
part is now specified by the keyword variable by
offind_element ()
.
For example, suppose you have the following code for Selenium3: (It is assumed that the driver executable file is in the PATH)
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://www.google.com/')
element = driver.find_element_by_xpath("//input[@type='text']")
element.send_keys('Qiita')
element.submit()
If this is made into the Selenium 4 specification, it will be as follows.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get('https://www.google.com/')
element = driver.find_element(by=By.XPATH, value="//input[@type='text']")
element.send_keys('Qiita')
element.submit()
Rewriting find_element
is quite troublesome, so if you have a lot of test code, it seems to be difficult unless you automate it as if you were writing a script.
Recommended Posts