python과 selenium에서 element를 찾을 때 사용하는 selector에 대해 알아보겠습니다.
By Class를 이용하여 다양한 방법으로 원하는 element를 찾을 수 있습니다.
class, id, css selector, xpath, name 등을 지원합니다.

일반적으로 element ID가 사용가능하고, 고유하며, 예측가능하다면 검색할 때 element ID를 통해 하는 것이 매우 빠르고, 복잡하게 DOM을 이용하는 방법들을 속도에서 앞섭니다.
고유한 ID가 없다면, CSS selector이 그 다음으로 선호되는 방법입니다.
XPath 또한 CSS selector만큼 잘 작동되지만, 구문이 복잡하며 디버깅을 하기 힘들고 꽤 느린 경향이 있다고 합니다.


selenium element selector By python

selenium selector By.id python

element id를 이용하여 찾는 방법입니다.


from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By import time driver = webdriver.Chrome('./chromedriver') driver.get("https://www.tistory.com/auth/login/old?redirectUrl=https%3A%2F%2Fwww.tistory.com%2F") login_input = driver.find_element_by_id("loginId") login_input.send_keys("user", "email") login_pw = driver.find_element_by_id("loginPw") login_pw.send_keys("password") time.sleep(2) # 비공개 메소드 사용법 login_input.clear() login_input = driver.find_element(By.ID, 'loginId') login_input.send_keys("by", "email") login_pw.clear() login_pw = driver.find_element(By.ID, 'loginPw') login_pw.send_keys("by_password") time.sleep(3) driver.close()

By.ID를 이용하여 element를 찾고 각각 값을 입력하였습니다.

결과

비공개 함수에서 제공하는 By 속성 속성은 아래와 같습니다.
상황에 맞게 두 가지 중 선택하여 사용하시면 됩니다.


ID = "id" XPATH = "xpath" LINK_TEXT = "link text" PARTIAL_LINK_TEXT = "partial link text" NAME = "name" TAG_NAME = "tag name" CLASS_NAME = "class name" CSS_SELECTOR = "css selector"


selenium selector By.className python

driver.get("https://www.tistory.com/auth/login/old?redirectUrl=https%3A%2F%2Fwww.tistory.com%2F") chk_login = driver.find_element_by_class_name("ico_check") chk_login.click() time.sleep(2) chk_login = driver.find_element(By.CLASS_NAME, 'ico_check') chk_login.click() time.sleep(3)

By.CLASS_NAME를 이용하여 element를 찾고 로그인 상태 유지 버튼을 클릭하고 해제하는 이벤트를 주었습니다.


selenium selector By.name python

driver.get("https://www.tistory.com/auth/login/old?redirectUrl=https%3A%2F%2Fwww.tistory.com%2F") login_input = driver.find_element_by_name("loginId") login_input.send_keys("userId") login_pw = driver.find_element(By.NAME, "password") login_pw.send_keys("password")

By.NAME를 이용하여 element를 찾고 각각 값을 입력하였습니다.


selenium selector By.xpath python

driver.get("https://www.tistory.com/auth/login/old?redirectUrl=https%3A%2F%2Fwww.tistory.com%2F") login_input = driver.find_element_by_xpath('//*[@id="loginId"]') login_input.send_keys("userId") login_pw = driver.find_element(By.XPATH, '//*[@id="loginPw"]') login_pw.send_keys("password")

By.XPATH를 이용하여 element를 찾고 각각 값을 입력하였습니다.

selenium에서 element를 찾을 때 사용하는 By 클래스에 대해 알아보았습니다.
By 클래스에서 제공하는 다양한 함수를 활용하면
element id, classname, name, css selector, xpath 등을 활용하여 원하는 element를 찾을 수 있습니다.
다음에는 원하는 페이지 로딩 혹은 element 로딩이 끝날때까지 기다리는 Wait에 대해 알아보겠습니다.


[Selenium] - selenium python 페이지 열기

[Selenium] - selenium WebElement python

'Selenium' 카테고리의 다른 글

selenium element timeout/wait By nodejs  (0) 2021.01.18
selenium WebElement python  (0) 2021.01.14
selenium python 페이지 열기  (0) 2021.01.13
selenium element selector By nodejs  (0) 2021.01.12
selenium element 찾기 nodejs  (0) 2021.01.12

+ Recent posts