don't stop believing

Chrome 브라우저 창 크기 조절 과 뒤로가기 앞으로 가기 본문

Testing Automation/Selenium

Chrome 브라우저 창 크기 조절 과 뒤로가기 앞으로 가기

Tongchun 2018. 3. 22. 18:57

selenium을 하면서 굳이~~ 창을 크게 하고 싶다면 두 가지 방법이 있습니다.

chrome option을 사용하는 방법과 maximize_window() 함수와 set_window_size() 함수를 사용하는 방법입니다.


chrome option을 사용하기 위해서는 Options 모듈을 아래와 같이 추가해야 합니다.

from selenium.webdriver.chrome.options import Options


먼저 chrome을 전체화면으로 키우는 옵션입니다. chrome에서 F11 키를 누르는 것과 동일합니다.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()

# chrome에서 F11을 눌러 전체 화면으로 넓히는 옵션입니다.
options.add_argument('--kiosk')

# executable_path에는 chromedriver 실행 파일의 경로를 넣고, chrome_options에는 options 변수를 넣습니다.
driver = webdriver.Chrome(executable_path='./chromedriver', chrome_options=options)

# 브라우저에서 google을 접속합니다.
driver.get('https://google.com')


다음은 전체 화면으로 키우는 옵션입니다.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()

# chrome을 전체화면으로 넓히는 옵션입니다.
options.add_argument('--start-fullscreen')

# executable_path에는 chromedriver 실행 파일의 경로를 넣고, chrome_options에는 options 변수를 넣습니다.
driver = webdriver.Chrome(executable_path='./chromedriver', chrome_options=options)

# 브라우저에서 google을 접속합니다.
driver.get('https://google.com')


Chrome 창을 원하는 넓이와 높이로 조정할 수 있습니다.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()

# executable_path에는 chromedriver 실행 파일의 경로를 넣고, chrome_options에는 options 변수를 넣습니다.
driver = webdriver.Chrome(executable_path='./chromedriver', chrome_options=options)

# chrome 창을 원하는 가로폭과 세로폭으로 저정합니다.
driver.set_window_size(1024, 600)

# 브라우저에서 google을 접속합니다.
driver.get('https://google.com')

브라우저에서 뒤로 가기와 앞으로 가기는 back()forward() 함수로 할 수 있습니다.

# -*- coding: utf-8 -*-
import time
from selenium import webdriver

# Chrome WebDriver를 이용해 Chrome을 실행합니다.
driver = webdriver.Chrome('./chromedriver')

# www.google.com으로 이동합니다.
driver.get('http://www.google.com')
time.sleep(2)

# html element 이름이 q인 것을 찾습니다. (검색창)
inputElement = driver.find_element_by_name('q')
time.sleep(2)

# 검색창에 'www.ngle.co.kr'을 입력합니다.
inputElement.send_keys('ngle')
time.sleep(2)

# 검색 내용을 보냅니다.
inputElement.submit()
time.sleep(2)

# 검색된 리스트 중 링크 텍스트에 'THE BEST BUSINESS PLAN'이 포함된 것을 찾습니다.
continue_link = driver.find_element_by_partial_link_text('THE BEST BUSINESS PLAN')
time.sleep(2)

# 해당 링크를 클릭합니다.
continue_link.click()
time.sleep(5)

# 페이지를 뒤로 감니다. (검색 결과 리스트를 다시 봅니다.)
driver.back()
time.sleep(5)

# ngle 웹 사이트로 다시 감니다.
driver.forward()
time.sleep(5)

여기까지 창 크기 조절과 뒤로가기, 앞으로가기 였습니다.


'Testing Automation > Selenium' 카테고리의 다른 글

Selenium으로 Alert 창 처리하기  (1) 2018.11.01
selenium과 Unittest 같이 사용하기  (0) 2018.03.28
iframe 처리하기  (4) 2018.03.22
element highlight로 확인해 보기  (0) 2018.03.22
Chrome에서 Tab 변경하기  (0) 2018.03.21
Comments