find_element(By.ID, "id")
find_element(By.NAME, "name")
find_element(By.XPATH, "xpath")
find_element(By.LINK_TEXT, "link text")
find_element(By.PARTIAL_LINK_TEXT, "partial link text")
find_element(By.TAG_NAME, "tag name")
find_element(By.CLASS_NAME, "class name")
find_element(By.CSS_SELECTOR, "css selector")
크롬 실행
# 메인 드라이브 찾기
import os
Download = os.path.join(os.path.expanduser('~'), 'downloads')
MainDirEx = Download.split(":")
MainDir = MainDirEx[0].replace("\\", "/")
MainDir = str(MainDir)+":/"
print(MainDir)
# result
C:
# 크롬 찾기
import re
ChromeExeFile = ""
def ChromeDriverSearch() :
ChromeExeFileU = "" # chrome.exe 경로
SetDir = str(MainDir)+"Users"
for (path, dir, files) in os.walk(str(SetDir)):
for filename in files:
ext = os.path.splitext(filename)
extfilename = filename
if extfilename == 'chrome.exe' and re.search("google", path.lower()) and re.search("chrome", path.lower()) :
ChromeExeFileU = "%s/%s" % (path, filename)
break
if ChromeExeFileU :
break
# User 없으면 c:
if not ChromeExeFileU :
SetDir = str(MainDir)
for (path, dir, files) in os.walk(str(SetDir)):
for filename in files:
ext = os.path.splitext(filename)
extfilename = filename
if extfilename == 'chrome.exe' and re.search("google", path.lower()) and re.search("chrome", path.lower()) :
ChromeExeFileU = "%s/%s" % (path, filename)
break
if ChromeExeFileU :
break
ChromeExeFileU = ChromeExeFileU.replace("\\", "/")
return ChromeExeFileU
ChromeExeFile = ChromeDriverSearch()
print(ChromeExeFile)
# result
C:/Program Files\Google\Chrome\Application/chrome.exe
# 크롬 브라우저
import socket
import subprocess
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.alert import Alert
Chorme_Dir = MainDir+"{원하는 폴더}"
def ChromeDriver(port) :
_Port_ = port
HOST = "127.0.0.1"
PORT = int(_Port_)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Try to connect to the given host and port
if sock.connect_ex((HOST, PORT)) == 0 :
pass
else:
# history 삭제
HistoryFile = str(Chorme_Dir)+"_Chrome"+str(PORT)+"/Default/History"
if os.path.exists(HistoryFile) :
try :
os.remove(HistoryFile)
except :
pass
if os.path.exists(ChromeExeFile) :
try :
sp = subprocess.Popen(str(ChromeExeFile)+' --remote-debugging-port='+str(PORT)+' --user-data-dir="'+str(Chorme_Dir)+'_Chrome'+str(PORT)+'"')
except :
return False
else :
return False
# Close the connection
sock.close()
# 브라우저 꺼짐 방지 옵션
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", HOST+":"+_Port_)
# 크롬 드라이버 생성
driver = webdriver.Chrome(options=chrome_options)
#driver.maximize_window() # 크롬 전체화면
driver.set_window_size(1400, 1000) # 크롬 사이즈
driver.set_window_position(0, 0) # 위치
return driver
deiver = ChromeDriver("80")
driver.get("{url}") # url 이동
driver.implicitly_wait(10) # 읽어올때까지 기다리기
driver.switch_to.frame("{name or id}") # iframe 변경
NowUrl = driver.current_url # 현재 url
source = driver.page_source # 현재 소스
Html = source.get_attribute('innerHTML') # html
# 특정 태그 뽑기
TestId = driver.find_element(By.ID, "{id}")
class = TestId.get_attribute("class") # TestId 의 class 값
value = TestId.get_attribute("value") # TestId 의 value 값
'IT > 프로그램' 카테고리의 다른 글
[python] pymysql (0) | 2025.03.14 |
---|---|
python - format / php - sprintf (0) | 2025.03.14 |
[php] 윈도우 스케줄러 cmd schtasks 사용 (0) | 2020.08.28 |
[scraping - php 스크래핑] 1. 상품정보 수집에 대한 정보 DB화 (2) | 2019.01.17 |
[scraping - php 스크래핑] 시작하기 (0) | 2019.01.17 |