LoginSignup
3
1

More than 1 year has passed since last update.

【Python】Selenium 4で、Selenium 3の書き方で書いたらWarningが出た

Last updated at Posted at 2021-10-29

はじめに

pythonでseleniumの使い方を学習していたら、Warningが出てきたので、調べてみると、自分がつかっているseleniumが4でした:sweat_smile:

selenium 4だと、書き方が多少異なるようですので、備忘録として残したいと思いました。

  • PC: M1 Mac book Air
  • OS: Big Sur ver11.6

DeprecationWarning: executable_path has been deprecated, please pass in a Service object

from selenium import webdriver
from selenium.webdriver.chrome.service import Service 

s = Service('/Users/ds/work/py_lesson/tools/chromedriver')
driver = webdriver.Chrome(service=s, options=options)

driver.get('https://news.yahoo.co.jp')

状況としては、py_lessonというディレクトリで、seleniumの学習をしています。
なので、toolsの中に、seleniumの公式ページからダウンロードしたchromedriverを置いています。

Service()の括弧の中に、chromedriverのパスを入れてから、Chrome()の引数として指定する感じですね。

UserWarning: find_element_by_* commands are deprecated. Please use find_element() instead

from selenium import webdriver
from selenium.webdriver.chrome.service import Service 
from selenium.webdriver.common.by import By


s = Service('/Users/ds/work/py_lesson/tools/chromedriver')
driver = webdriver.Chrome(service=s)

driver.get('https://news.yahoo.co.jp')

e = driver.find_element(By.TAG_NAME, "h2")

学習では、find_element_by_tag_nameを使っていましたが、どうやら、find_elementの括弧の中身は、By.IDBy.TAG_NAMEといったBy.xxxを使うようです。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1