Python/Python Error

[Error] Selenium - AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'

도도걸만단 2024. 10. 8. 22:25
반응형

오랜만에 크롤링 할 일이 생겨 셀레니움을 쓰려고하는데 시작부터 에러가 났다.

AttributeError: 'WebDriver' object as no attribute 'find_element_by_xpath'

에러 ㅠㅠ

알고보니 버전 에러 문제라고 한다. 코드 작성 방식을 조금 바꿔야 한다.

1. 모듈 추가

from selenium.webdriver.common.by import By

2. 코드 변경

find_element_by_xpath('') find_element(By.XPATH, '')

 

find_element_by_xpath("")를 find_element(By.XPATH, "")형태로 변경해준다.
다른 속성도 마찬가지.



그러니까 마지막 두줄이 문제란거지

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()


# 수강신청 사이트 접속
driver.get('https://sugang.inha.ac.kr/sugang/')
time.sleep(1)

# 수강신청 사트 로그인
id_box = driver.find_element_by_xpath('//*[@id="txtCode"]')
pw_box = driver.find_element_by_xpath('//*[@id="txtPassword"]')

 

이렇게 바꾸면 됨.

id_box = driver.find_element(By.XPATH, '//*[@id="txtCode"]')
pw_box = driver.find_element(By.XPATH, '//*[@id="txtPassword"]')

 

반응형