๋ฐ์ํ
find_element_by_id(), find_element_by_class() ์๋จนํ์ ๋ด๊ฑฐ ์จ์ผํจ
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, " ")
driver.find_elements(By.XPATH, " ")
driver.find_element(By.CLASS_NAME, " ")
driver.find_elements(By.CLASS_NAME, " ")
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")
tab ๋ชฉ๋ก ๋ณด๊ธฐ
driver.window_handles
tab ์ ํ
last_tab = driver.window_handles[-1]
driver.switch_to.window(window_name=last_tab)
์ฐฝ ๋ซ๊ธฐ
driver.close()
first_tab = driver.window_handles[0]
driver.switch_to.window(first_tab)
๋ฐ์ํ
'Python > ์คํฌ๋กค๋ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] Pyppeteer (0) | 2022.07.25 |
---|---|
[Python] ํฌ๋กค๋ง Bookmarks (0) | 2022.07.24 |