[Python] Selenium

Jen Y·2021년 8월 5일
0

Python

목록 보기
15/17

7. WebDriver API

7.21. Remote WebDriver WebElement

메소드 & 필드..

clear()
click()
find_element(by='id', value=None)
.
.
.
location
location_once_scrolled_into_view
parent
rect
screenshot
screenshot_as_base64
screenshot_as_png
send_keys
size
submit()
tag_name
text
value_of_css_property

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(url)

id = 'id'
pw = '***'

id_text = browser.find_element_by_id("id")
pw_text = browser.find_element_by_id("password")

id_text.clear()
id_text.send_keys(id)

pw_text.clear()
pw_text.send_keys(pw)

# enter
pw_text.send_keys(Keys.RETURN)

    

Locating Elements (요소 찾기)

find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

To find multiple elements (these methods will return a list):

여러 요소 찾을때, list로 반환됨

find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector

Apart from the public methods given above, there are two private methods which might be useful for locating page elements:

find_element
find_elements

Example usage:

from selenium.webdriver.common.by import By

driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')

These are the attributes available for By class:

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"


모든 요소 검색! find all elements

  • xpath로 찾기 : find_elements_by_xpath
driver.find_elements_by_xpath(".//*")

find elements by id

driver.find_elements_by_id("id")

find element by id

driver.find_element_by_id("id")

execute javascript

driver.execute_script('code')

# scrollTop += 50
driver.execute_script('return document.getElementsByClassName("y-scroll")[1].scrollTop += 50')

id = browser.execute_script('return info.id')

# click
driver..execute_script('return document.getElementsByClassName("sadfsdafsdaf")[0].click()')

get_property

div = driver.find_elements_by_id("div")
div[0].get_property("scrollTop")

0개의 댓글