행위

"Chrome자동화"의 두 판 사이의 차이

DB CAFE

(새 문서: Selenium Headless WebDriver requirements Since we don’t have a screen to run Firefox we are going to be using Xvfb to simulate a display and run everything in memory. apt-get insta...)
 
 
(같은 사용자의 중간 판 3개는 보이지 않습니다)
1번째 줄: 1번째 줄:
Selenium Headless WebDriver requirements
+
https://selenium-python.readthedocs.io/index.html
Since we don’t have a screen to run Firefox we are going to be using Xvfb to simulate a display and run everything in memory.
 
  
 +
Selenium Headless WebDriver 요구사항
 +
 +
1. Xvfb를 설치하여 메모리에서 화면을 시물레이션
 +
 +
<source lang=python>
 
apt-get install xvfb
 
apt-get install xvfb
Now we are going to install Firefox and few requirements that maybe already installed on your system but here are anyway:
+
</source>
 +
 
 +
 
 +
2. 파이어폭스 설치 및 요구사항 설치
  
 +
<source lang=python>
 
echo -e "\ndeb http://downloads.sourceforge.net/project/ubuntuzilla/mozilla/apt all main" | tee -a /etc/apt/sources.list > /dev/null
 
echo -e "\ndeb http://downloads.sourceforge.net/project/ubuntuzilla/mozilla/apt all main" | tee -a /etc/apt/sources.list > /dev/null
 
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com C1289A29
 
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com C1289A29
12번째 줄: 20번째 줄:
 
apt-get install libgtk2.0-0
 
apt-get install libgtk2.0-0
 
apt-get install libasound2
 
apt-get install libasound2
Now we need PyVirtualDisplay which is a python wrapper for Xvfb used for easy working with virtual displays in python.
+
</source>
  
 +
 +
3. 파이썬 화면가상화 (Xvfb를 이용 손쉽게 하기 위함 )
 +
<source lang=python>
 
pip install pyvirtualdisplay
 
pip install pyvirtualdisplay
Now we are going to install selenium as a final component for our project:
+
</source>
  
 +
4.셀리니엄 설치
 +
<source lang=python>
 
pip install selenium
 
pip install selenium
Now here is a simple python script to take a screenshot from a website:
+
</source>
  
 +
 +
5.파이썬 실행 예제 스크립트
 
<source lang=python>
 
<source lang=python>
  
56번째 줄: 71번째 줄:
 
This will create a file vionblog.png with the screenshot of http://www.vionblog.com front page taken  
 
This will create a file vionblog.png with the screenshot of http://www.vionblog.com front page taken  
 
with screen resolution of 1366 x 768
 
with screen resolution of 1366 x 768
 +
 +
== 크롬 headless 모드 ==
 +
<source lang=python>
 +
chrome_options = webdriver.ChromeOptions()  # 크롬 옵션 객체 생성
 +
chrome_options.add_argument('headless')  # headless 모드 설정
 +
chrome_options.add_argument("--disable-gpu")  # gpu 사용 안하도록 설정
 +
chrome_options.add_argument("lang=ko_KR")  # 한국어로 실행되도록 설정
 +
 +
# 추가된 부분, UserAgent값을 바꿔 headless 모드임을 숨김
 +
chrome_options.add_argument(
 +
"user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
 +
 +
driver = webdriver.Chrome('C:/chromedriver/chromedriver.exe', chrome_options=chrome_options)
 +
driver.implicitly_wait(10)
 +
driver.get("https://naver.com")
 +
driver.implicitly_wait(10)
 +
print(driver.find_element_by_xpath('//*[@id="account"]/div/p').text)
 +
</source>
 +
[[Category:python]]

2020년 4월 4일 (토) 12:23 기준 최신판

thumb_up 추천메뉴 바로가기


https://selenium-python.readthedocs.io/index.html

Selenium Headless WebDriver 요구사항

1. Xvfb를 설치하여 메모리에서 화면을 시물레이션

apt-get install xvfb


2. 파이어폭스 설치 및 요구사항 설치

echo -e "\ndeb http://downloads.sourceforge.net/project/ubuntuzilla/mozilla/apt all main" | tee -a /etc/apt/sources.list > /dev/null
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com C1289A29
apt-get update
apt-get install firefox-mozilla-build
apt-get install libdbus-glib-1-2
apt-get install libgtk2.0-0
apt-get install libasound2


3. 파이썬 화면가상화 (Xvfb를 이용 손쉽게 하기 위함 )

pip install pyvirtualdisplay

4.셀리니엄 설치

pip install selenium


5.파이썬 실행 예제 스크립트

#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

# Set screen resolution to 1366 x 768 like most 15" laptops
display = Display(visible=0, size=(1366, 768))
display.start()

# now Firefox will run in a virtual display.
browser = webdriver.Firefox()

# Sets the width and height of the current window
browser.set_window_size(1366, 768)

# Open the URL
browser.get('http://www.vionblog.com/')

# set timeouts
browser.set_script_timeout(30)
browser.set_page_load_timeout(30) # seconds

# Take screenshot
browser.save_screenshot('vionblog.png')

# quit browser
browser.quit()

# quit Xvfb display
display.stop()

This will create a file vionblog.png with the screenshot of http://www.vionblog.com front page taken with screen resolution of 1366 x 768

크롬 headless 모드[편집]

chrome_options = webdriver.ChromeOptions()  # 크롬 옵션 객체 생성
chrome_options.add_argument('headless')  # headless 모드 설정
chrome_options.add_argument("--disable-gpu")  # gpu 사용 안하도록 설정
chrome_options.add_argument("lang=ko_KR")  # 한국어로 실행되도록 설정

# 추가된 부분, UserAgent값을 바꿔 headless 모드임을 숨김
chrome_options.add_argument(
"user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")

driver = webdriver.Chrome('C:/chromedriver/chromedriver.exe', chrome_options=chrome_options)
driver.implicitly_wait(10)
driver.get("https://naver.com")
driver.implicitly_wait(10)
print(driver.find_element_by_xpath('//*[@id="account"]/div/p').text)