Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- postgresql
- create table
- Materials
- GoCD
- Jupyter
- centos
- 28015
- ftp
- ssh
- PYTHON
- 실행권한
- kitura
- STF
- perfect
- nmap
- nGrinder
- sshpass
- port forwarding
- Jupyter Notebook
- mysql
- insert
- nohup
- postgres
- openpyxl
- rethinkdb
- appium server
- appium
- ubuntu
- STF_PortForwarding
- SWIFT
Archives
- Today
- Total
don't stop believing
element highlight로 확인해 보기 본문
Selenium Code를 작성할때 element를 잘 찾았는지 확인해 보고 싶을때가 있습니다.
SikuliX를 배울때는 highlight 함수가 있어 편하게 확인했는데 Selenium에서는 함수를 하나 만들어 줘야 합니다.
selenium의 webdriver 클레스에 execute_script() 함수가 있습니다. 이걸 사용하면 현재 열려진 화면에 css를 수정하거나 할 수 있습니다.
함수는 아래와 같습니다.
123456789101112131415161718192021def highlight(element):# 파라메터로 전달받은 element의 상위 driver를 확인합니다.driver = element._parent# highlight() 함수안에 apply_style()이라는 내부 함수를 만들어 줍니다.def apply_style(s):# execute_script() 함수를 사용해 해당 element의 css를 추가해 줍니다.driver.execute_script("arguments[0].setAttribute('style', arguments[1]);", element, s)# 원래 스타일을 확인하고 저장해 놉니다.original_style = element.get_attribute('style')# 내부 함수인 apply_style로 css를 적용합니다.# 바탕은 노란색, 외곽선은 붉은색오로 합니다.apply_style("background: yellow; border: 2px solid red;")# 스타일을 변경한 채로 2초간 대기합니다.time.sleep(2)# 다시 원래 스타일로 적용합니다.apply_style(original_style)
위 highlight 함수를 기본 스크립트에 적용해 봅시다.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849# -*- coding: utf-8 -*-import timefrom selenium import webdriver# selenium 기본 코드에 hightlight() 함수를 적용합나다.def highlight(element):driver = element._parentdef apply_style(s):driver.execute_script("arguments[0].setAttribute('style', arguments[1]);", element, s)original_style = element.get_attribute('style')apply_style("background: yellow; border: 2px solid red;")time.sleep(2)apply_style(original_style)# Chrome WebDriver를 이용해 Chrome을 실행합니다.driver = webdriver.Chrome('./chromedriver')# www.google.com으로 이동합니다.driver.get("www.google.com")time.sleep(2)# html element 이름이 q인 것을 찾습니다. (검색창)inputElement = driver.find_element_by_name("q")# htghlight() 함수로 inputElement를 확인해 봅니다.highlight(inputElement)time.sleep(2)# 검색창에 'www.ngle.co.kr'을 입력합니다.inputElement.send_keys("www.ngle.co.kr")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')# htghlight() 함수로 continue_link 확인해 봅니다.highlight(continue_link)time.sleep(2)# 해당 링크를 클릭합니다.continue_link.click()time.sleep(5)# WebDriver를 종료합니다. (브라우저 닫기)driver.quit()
highlight 함수를 만들어서 element를 확인하는 방법이었습니다.
'Testing Automation > Selenium' 카테고리의 다른 글
Chrome 브라우저 창 크기 조절 과 뒤로가기 앞으로 가기 (0) | 2018.03.22 |
---|---|
iframe 처리하기 (4) | 2018.03.22 |
Chrome에서 Tab 변경하기 (0) | 2018.03.21 |
Selenium IDE (Chrome Extension) 사용하기 (0) | 2018.03.19 |
Webdriver 실행하기 (0) | 2018.03.19 |