don't stop believing

Selenium (Python) 설치와 기본 사용해 보기 (Mac) 본문

Testing Automation/Selenium

Selenium (Python) 설치와 기본 사용해 보기 (Mac)

Tongchun 2017. 11. 6. 15:36

Selenium은 어려 개발 언어를 지원합니다. python 버전을 설치하고 기본 예제를 실행해 봅시다.

Python이 설치되어 있다면 pip으로 간단히 설치할 수 있습니다.

1
2
3
4
5
6
$ pip install selenium
Collecting selenium
Downloading selenium-3.7.0-py2.py3-none-any.whl (935kB)
100% |████████████████████████████████| 942kB 1.6MB/s
Installing collected packages: selenium
Successfully installed selenium-3.7.0
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Mac에서 selenium을 실행한다면 geckodriver를 설치하고 설치된 위치를 확인합니다.

1
2
3
$ brew install geckodriver
$ which geckodriver
/usr/local/bin/geckodriver
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

아래와 같이 코드를 작성하고 실행하면 Firefox를 실행하고 google을 열어 검색창에 cheese!를 입력하게 됩니다.

Firefox가 설치되어 있어야 합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
import time
# Create a new instance of the Firefox driver
driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
# go to the google home page
driver.get("http://www.google.com")
# the page is ajaxy so the title is originally this:
print(driver.title)
# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_name("q")
# type in the search
inputElement.send_keys("cheese!")
# submit the form (although google automatically searches now without submitting)
inputElement.submit()
time.sleep(5)
try:
# we have to wait for the page to refresh, the last thing that seems to be updated is the title
WebDriverWait(driver, 10).until(EC.title_contains("cheese!"))
# You should see "cheese! - Google Search"
print(driver.title)
time.sleep(5)
finally:
driver.quit()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Chrome에서 실행하고 싶다면 CrhomeDriver를 다운로드 합니다.

[https://sites.google.com/a/chromium.org/chromedriver/downloads]


그리고 아래와 같이 코드를 작성해 실행합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import time
from selenium import webdriver
# Optional argument, if not specified will search path.
driver = webdriver.Chrome('/Users/appium/Documents/SeleniumTest/chromedriver')
driver.get('http://www.google.com/xhtml');
time.sleep(5)
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5)
driver.quit()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

만약 실행 후 아래와 같은 메시지가 나올 경우 ChromeDriver 버전을 확인해야 합니다.

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 80

현재 설치된 Chrome 브라우저 버전과 ChromeDriver의 버전은 동일해야 합니다.


보통 Selenium은 unittest와 함께 사용됩니다.

unittest와 함께 사용하는 기본 코드는 아래와 같습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
def test_search_in_python_org(self):
driver = self.driver
driver.get("http://www.python.org")
self.assertIn("Python", driver.title)
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


[http://selenium-python.readthedocs.io/]

Comments