programing

권장되지 않음 경고: executable_path가 권장되지 않는 셀레늄 파이썬입니다.

css3 2023. 6. 24. 09:27

권장되지 않음 경고: executable_path가 권장되지 않는 셀레늄 파이썬입니다.

나는 sublime을 사용하여 python 스크립트를 코딩하고 있습니다.다음 코드는 python의 selenium이 webdriver_manager 패키지를 사용하여 드라이버를 자동으로 설치하기 위한 코드입니다.

# pip install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()

#s=Service(path)
#driver=webdriver.Chrome(service=s)
driver.get('https://www.google.com')
driver.find_element(By.NAME, 'q').send_keys('Yasser Khalil')

코드는 정상적으로 작동하지만 저는 그런 경고를 받았습니다.

Demo.py:7: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(ChromeDriverManager().install())

그런 버그를 어떻게 고칠까요?

이 오류 메시지...

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

...다음 릴리스에서는 키가 더 이상 사용되지 않을 것으로 예상됩니다.

이 변경 사항은 다음과 같이 언급된 셀레늄 4.0 베타 1 변경 로그와 일치합니다.

다음을 제외한 모든 사용 안 함Options그리고.Service드라이버 인스턴스화의 인수입니다.(#9125,#9128)


해결책

셀레늄4가executable_path 더 이상 사용되지 않는 경우, 당신은 다음의 인스턴스를 사용해야 합니다.Service()와 같은 부류.ChromeDriverManager().install()명령은 아래에 설명된 대로입니다.

사전 요구 사항

다음 사항을 확인합니다.

  • 셀레늄v4.0.0으로 업그레이드됨

    pip3 install -U selenium
    
  • Python용 Webdriver Manager가 설치됨

    pip3 install webdriver-manager
    

ModuleNotFoundError: 드라이버 관리자를 설치한 후에도 'webdriver_manager'라는 이름의 모듈이 없습니다.

셀레늄 v4 호환 코드 블록

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

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.com")

콘솔 출력:

[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 96.0.4664
[WDM] - Get LATEST driver version for 96.0.4664
[WDM] - Driver [C:\Users\Admin\.wdm\drivers\chromedriver\win32\96.0.4664.45\chromedriver.exe] found in cache

Python용 Webdriver Manager를 사용하여 Selenium ChromeDriver 문제Python용 Webdriver Manager 설치에 대한 자세한 내용을 확인할 수 있습니다.


사용할 수 있는 개체를 전달하려는 경우:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.google.com")

TL; DR

다음에서 관련 버그 보고서/풀 요청을 찾을 수 있습니다.

이것은 나에게 효과가 있습니다.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(r"C:\chromedriver.exe")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)

승인된 응답에 대해 확장된 서비스 클래스는 이전에 executable_path 매개 변수를 사용한 것과 동일한 방법으로 ChromeDriver 실행 파일을 명시적으로 지정할 수 있습니다.이러한 방식으로 기존 코드를 쉽게 마이그레이션할 수 있습니다(분명히 교체해야 함).C:\chromedriver.exe당신의 길 위에).

내가 해결할 수 있었습니다.

# pip install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.maximize_window()
driver.get('https://www.google.com')
driver.find_element(By.NAME, 'q').send_keys('Yasser Khalil')

셀레늄, Pip 및 Python 업데이트에서 이 권장되지 않는 문제가 발생하고 있음을 발견했습니다.그래서 간단히 변경:

이전:

from selenium import webdriver
chrome_driver_path = 'C:/Users/Morteza/Documents/Dev/chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver_path)

url = "https://www.google.com"
driver.get(url)

이후:

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

s=Service('C:/Users/Morteza/Documents/Dev/chromedriver.exe')
browser = webdriver.Chrome(service=s)
url='https://www.google.com'
browser.get(url)

위의 모든 답변은 Chrome을 참조하며 Firefox용 답변을 추가합니다.

설치:

pip install webdriver-manager

코드:

from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(service=Service(executable_path=GeckoDriverManager().install()))

참조: https://github.com/SergeyPirogov/webdriver_manager/issues/262#issuecomment-955197860

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service_obj = Service("WebDrivers_path\chromedriver.exe")
driver = webdriver.Chrome(service=service_obj)
driver.get("https://www.google.com")

여기에서 서비스 개체의 새 정의를 확인하십시오.

나의 해결책

from selenium.webdriver.chrome.service import Service

chrome_executable = Service(executable_path='chromedriver.exe', log_path='NUL')
driver = webdriver.Chrome(service=chrome_executable)

Chrome 자동 설치 프로그램의 가장 간단한 옵션:

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

chromedriver_autoinstaller.install()
driver = webdriver.Chrome(service=Service())

PyCharm과 같은 IDE를 사용하는 경우 해당 IDE의 웹 드라이버-매니저 패키지를 셀레늄 패키지에 설치하려면 어떻게 해야 합니까?

ChromeDriver별 기능을 편리하게 설정할 수 있는 ChromeOptions 인스턴스를 만들 수 있습니다.그런 다음 ChromeOptions 개체를 ChromeDriver 생성자로 전달할 수 있습니다.

ChromeOptions options = new ChromeOptions();

options.addExtensions(new File("/path/to/extension.crx"));

ChromeDriver driver = new ChromeDriver(options);

Selenium 버전 3.6.0 이후 Java의 Chrome Options 클래스는 Capabilities 인터페이스도 구현하여 ChromeDriver에만 해당되지 않는 다른 WebDriver 기능을 지정할 수 있습니다.

ChromeOptions options = new ChromeOptions();

// Add the WebDriver proxy capability.

Proxy proxy = new Proxy();

proxy.setHttpProxy("myhttpproxy:3337");

options.setCapability("proxy", proxy);

// Add a ChromeDriver-specific capability.

options.addExtensions(new File("/path/to/extension.crx"));

ChromeDriver driver = new ChromeDriver(options);

언급URL : https://stackoverflow.com/questions/64717302/deprecationwarning-executable-path-has-been-deprecated-selenium-python