๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Python/์Šคํฌ๋กค๋ง

[Python] Selenium

๋ฐ˜์‘ํ˜•

 

 

https://stackoverflow.com/questions/72773206/selenium-python-attributeerror-webdriver-object-has-no-attribute-find-el

 

Selenium - Python - AttributeError: 'WebDriver' object has no attribute 'find_element_by_name'

I am trying to get Selenium working with Chrome, but I keep running into this error message (and others like it): AttributeError: 'WebDriver' object has no attribute 'find_element_by_name' The same

stackoverflow.com

 

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

 

7. WebDriver API — Selenium Python Bindings 2 documentation

A single IP address, as a string. If any IPv4 address is found, one is returned. Otherwise, if any IPv6 address is found, one is returned. If neither, then None is returned.

selenium-python.readthedocs.io

๋ฉ”์†Œ๋“œ & ํ•„๋“œ..

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 (์š”์†Œ ์ฐพ๊ธฐ)

 

4. Locating Elements — Selenium Python Bindings 2 documentation

There are various strategies to locate elements in a page. You can use the most appropriate one for your case. Selenium provides the following methods to locate elements in a page: Apart from the public methods given above, there are two private methods wh

selenium-python.readthedocs.io

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