0
0

More than 1 year has passed since last update.

Windows 10 + Python3 + selenium + chromedriver + headless chrome で Fessの検索画面を操作してみる

Last updated at Posted at 2020-06-08

目的

ドロップダウンの操作もかねてFessの検索画面を操作してみる
Freeはローカルで動かしているものを使用
Windows 10 Pro x64 + Python3 で selenium + chromedriver でFessの検索画面を操作してみる
を headless chrome を使用するように書き換えてみる

サンプルコード

対象とするhtmlの抜粋


<h3 id="searchOptionsLabel">
	検索オプション
</h3
ドロップダウン
<fieldset class="form-group">
	<label for="contentNum">表示件数</label>
	<select name="num" id="numSearchOption" class="form-control"><option value="10">
			- 表示件数 -
		</option>
		<option value="10" selected="selected">10</option>
		<option value="20">20</option>
		<option value="30">30</option>
		<option value="40">40</option>
		<option value="50">50</option>
		<option value="100">100</option></select>
</fieldset>
検索ワードの入力
<div class="clearfix">
	<div class="mx-auto col-10 col-sm-8 col-md-8 col-lg-6">
		<input type="text" name="q" maxlength="1000" size="50" value="" 
		id="contentQuery" class="query form-control center-block"
		autocomplete="off">
	</div>
</div>
検索ボタンクリック
<button type="submit" name="search" id="searchButton"
	class="btn btn-primary">
	<i class="fa fa-search"></i>
	検索
</button>
検索結果
<h3 class="title text-truncate">
 <a class="link" href="http://xxx.xxx.xxx.xxx/pgsql12/monitoring-ps.html"
 data-uri="http://xxx.xxx.xxx.xxx/pgsql12/monitoring-ps.html"
 data-id="f134efea76bf4e019bb4dac4fb9b87e4" data-order="0">27.1. 標準的なUnixツール</a>
</h3>

コード

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import chromedriver_binary

# ブラウザーを起動
options = Options()
options.binary_location = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)

driver.get('http://xxx.xxx.xxx.xxx:8080')

try:
    # 検索オプション 画面の表示
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'searchOptionsButton'))
    )
    element.click()

    # 表示件数の選択
    elms = WebDriverWait(driver, 4).until(
        EC.presence_of_element_located((By.ID, 'numSearchOption'))
    )
    elm = Select(elms).select_by_value('20')
    em = driver.find_element_by_name('num')
    print('表示件数:' + em.get_attribute('value'))
    # 表示件数:20

    # 検索ワードの入力
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'contentQuery'))
    )
    element.send_keys('Postgres')
    element.submit()

    # 検索ボタンクリック
    elm = driver.find_element_by_id('searchButton')
    elm.click()

    # 検索結果を取得する->classに記述されている属性のリストが作成される
    em = driver.find_elements_by_class_name('link')
    for item in em:
        print(item.get_attribute('data-order').rjust(2, '0') + ':' \
             +item.get_attribute('href')) 
    # 00:http://xxx.xxx.xxx.xxx/pgsql12/monitoring-ps.html
    # ~
    # 19:http://xxx.xxx.xxx.xxx/pgsql12/docguide-build.html

    time.sleep(2) # 表示確認用のwait
finally:
    driver.quit()

参考にしたのは以下のサイト

Windows 10 Pro x64 + Python3 で selenium + chromedriver でFessの検索画面を操作してみる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0