셀레니움으로 하고 싶은 것
- node 선택 : x1 ~ x20
- jenkins node들의 빌드 기록 페이지로 접근(/build/computer/x1/builds)
- table id="projectStatus", a tag class="model-link inside"의 href 가져오기
- href 클릭
PC 정보
- windows 10
- python 3.8.5
- pip 20.2.2
0. 환경 설정
- python3 설치 : 공식홈 다운로드
- pip 설치
python 설치된 폴더\Script로 가서 pip freeze
pip upgrade(필요하면)
> pyhton get-pip.py
> cd C:\Python\Python38-32\Scripts
> pip freeze
> python -m pip install --upgrade pip
- python-poetry 설치
참고 : spoqa.github.io/2019/08/09/brand-new-python-dependency-manager-poetry.html
(PS)> (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python
- poetry 설정
> poetry init
This command will guide you through creating your pyproject.toml config.
Package name [selenium]: build-data
Version [0.1.0]:
Description []:
Author [ny0011, n to skip]:
License []:
Compatible Python versions [^3.8]:
Would you like to define your main dependencies interactively? (yes/no) [yes]
Search for package to add (or leave blank to continue):
Would you like to define your development dependencies interactively? (yes/no) [yes] no
Generated file
[tool.poetry]
name = "build-data"
version = "0.1.0"
description = ""
authors = ["ny0011"]
[tool.poetry.dependencies]
python = "^3.8"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
Do you confirm generation? (yes/no) [yes]
- poetry로 selenium 추가 후 설치
> poetry add selenium
Using version ^3.141.0 for selenium
Updating dependencies
Resolving dependencies...
Writing lock file
Package operations: 2 installs, 0 updates, 0 removals
- Installing urllib3 (1.25.10)
- Installing selenium (3.141.0)
> poetry install
- vscode의 python package를 설치하면 잘 안되는 것 같다...
pip install selenium pylint webdriver_manager
1. webdriver 설정
- webdriver_manager 사용 : github.com/SergeyPirogov/webdriver_manager
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
browser = webdriver.Chrome(ChromeDriverManager().install())
2. 내가 원하는 web page 열기(jenkins node들의 빌드 기록 페이지로 접근)
browser.get("http://[jenkins url]/build/computer/{NODE}/builds")
3. 원하는 CSS selector가 로딩 될 때까지 기다리기
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, css_selector)))
4. 클릭할 요소 선택(table id="projectStatus", a tag class="model-link inside"의 href 가져오기)
build_table = browser.find_element_by_id("projectStatus")
build_list = build_table.find_elements_by_class_name("inside")
5. 새 탭으로 열리도록 클릭하기(href 클릭)
// 이전 탭 저장
windows_before = browser.current_window_handle
// 클릭
build_no.send_keys(Keys.CONTROL +"\n")
// 탭이 열리길 기다림
WebDriverWait(browser, 10).until(EC.number_of_windows_to_be(2))
// 새 탭 찾기
windows_after = browser.window_handles
new_window = [x for x in windows_after if x != before_window][0]
6. 스크랩 후 새 탭 닫고 이전 탭으로 이동
// 탭 닫기
browser.close()
// 탭 이동
browser.switch_to.window(windows_before)
// 탭이 1개가 되었는지 기다림
WebDriverWait(browser, 5).until(EC.number_of_windows_to_be(1))
'개발 > Python' 카테고리의 다른 글
[codility] MaxCounters (0) | 2021.02.17 |
---|---|
python requests로 웹 페이지 크롤링 하기 (0) | 2020.12.11 |
댓글