【windows7】python으로 Chrome의 브라우저 조작을 자동화 ~Selenium WebDriver를 이용
10424 단어 python2.7Chrome셀레늄selenium-webdriver
환경
windows7 32bit
python2.7.13
사전 준비
python2.7 설치
htps //w w. py 응. 오 rg / 도 w 응 아 ds / 에서 Download the latest version for Windows에서 Download Python 2.7.13을 클릭하십시오.

다운로드한 「python-2.7.13.msi」를 더블 클릭하여 인스톨러를 실행.
기본값은 "C:\Python27"에 설치되어야 합니다.
환경 변수 설정
설치한 파이썬의 path를 설정한다.
Windows 시작 메뉴에서 [컴퓨터] -> [속성] -> [시스템 고급 설정] -> [환경 변수]에서 시스템 환경 변수의 "Path"끝에 ";C:\Python27"을 추가합니다.
파이썬 설치 확인
명령 프롬프트 또는 powershell에서 다음 명령을 실행하여 버전이 표시됩니다.
> python.exe --version
Python 2.7.13
Selenium WebDriver 설정
Selenium WebDriver 설치
htp // // cs. 세니우 mhq. 오 rg / 도 w 응 아 d / 에서 "Selenium Client & WebDriver Language Bindings"의 "Python3.0.0.b2"의 다운로드 링크를 클릭하고 다음 페이지에서 "selenium-3.0.2.tar.gz"를 다운로드하십시오.


다운로드한 「selenium-3.0.2.tar.gz」를 해동하고, 그 중에서, 「selenium-3.0.2\py\selenium」의 「selenium」폴더마다, 「C:\Python27\Lib」의 바로 아래에 코피페.
chromedriver 설치
FireFox라면 디폴트로 문제 없지만 Chrome의 경우는 driver가 필요하다.
h tp // ch로메 d리ゔぇr. s 가시. ㅇㅜㅜㅜㅜ 이 m/그리고 x. HTML? Path = 2.25 / 부터,
자신의 환경에 있던 파일을 다운로드합니다. (이번에는 chromedriver_win32.zip 다운로드)
"chromedriver_win32.zip"에있는 "chromedriver.exe"
「C:\selenium\」의 바로 아래에 복사한다. (selenium 디렉토리가 없는 경우는 작성한다.)
자동 조작을 위한 프로그램 작성
크롬 브라우저를 열고 사이트의 버튼을 클릭하고 스크린 샷을 찍고 브라우저를 닫는 샘플 프로그램.
test_webdriver.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
class TestWebdriver(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome('C:\selenium\chromedriver')
self.driver.implicitly_wait(30)
self.base_url = "http://docs.seleniumhq.org/"
self.verificationErrors = []
self.accept_next_alert = True
def test_webdriver(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_link_text("Documentation").click()
driver.find_element_by_link_text("Selenium WebDriver").click()
driver.find_element_by_xpath("//input[@value='python']").click()
driver.save_screenshot("screenshot.png")
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
다음은 브라우저를 지정합니다.
self.driver = webdriver.Chrome('C:\selenium\chromedriver')
FireFox의 경우 경로 지정없이 갈 수있는 것 같습니다.
例
self.driver = webdriver.Firefox()
실행
powershell 또는 명령 프롬프트에서 명령 실행
> python test_webdriver.py
OK <- うまく実行されればokがでる
Reference
이 문제에 관하여(【windows7】python으로 Chrome의 브라우저 조작을 자동화 ~Selenium WebDriver를 이용), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ynishimura0922/items/a3332b5beb394248e44e텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)