Selenium
2020. 12. 14. 23:13ㆍ개발자료/Python
반응형
# Chrome 알림(Notification) 뜨지 않게 하기
chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
## or
prefs = {"profile.default_content_setting_values.notifications" : 2}
## 1:allow notification, 2:block notification
chrome_options.add_experimental_option("prefs",prefs)
## 첫번째(하나의) Element 반환
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
## 모든 Element 반환
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
## find_element By.* 사용
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_element(By.XPATH, '//button')
driver.find_element(By.ID, 'loginForm')
driver.find_element(By.LINK_TEXT, 'Continue')
driver.find_element(By.PARTIAL_LINK_TEXT, 'Conti')
driver.find_element(By.NAME, 'username')
driver.find_element(By.TAG_NAME, 'h1')
driver.find_element(By.CLASS_NAME, 'content')
driver.find_element(By.CSS_SELECTOR, 'p.content')
driver.find_elements(By.ID, 'loginForm')
driver.find_elements(By.CLASS_NAME, 'content')
#### Chrome
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chromedriver_path = "/chromedriver"
options = Options()
options.add_argument("--headless")
## user-agent
options.add_argument("--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36")
## retina 대응
options.add_argument("--force-device-scale-factor=1")
## 창 크기 설정
options.add_argument("--window-size=1100,1000")
## 보안 기능인 샌드박스를 비활성
options.add_argument("--no-sandbox")
driver = webdriver.Chrome(executable_path=chromedriver_path, options=options)
driver.get("https://google.com")
driver.quit()
#### FireFox (검증안됨)
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument('-headless')
driver = webdriver.Firefox(options=options)
driver.get("https://www.google.com/")
driver.close()
반응형
'개발자료 > Python' 카테고리의 다른 글
fake-useragent (0) | 2021.04.29 |
---|---|
경로(Path), 파일명, 확장자 조작 함수 (0) | 2021.03.12 |
pyserial - Python Serial Port Extension (0) | 2020.10.15 |
String - 문자열 (0) | 2020.09.18 |
Dictionary - 딕셔너리 (0) | 2020.04.02 |