LoginSignup
0
0

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-02-14

目的

ドロップダウンの操作もかねてFessの検索画面を操作してみる
Freeはローカルで動かしているものを使用
※公開されているデモ・サーバーはあるんだろうか?
※TODO:ローカルファイルもブラウザで開く気がしたんだけどDLになる

サンプルコード

対象とする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>
検索結果
<a class="link"
     href="file://var/export/web/fess/pgsql11/runtime.html"
     data-uri="file://var/export/web/fess/pgsql11/runtime.html"
     data-id="1d87eea3d06a44beb5301018894d24f7" data-order="0">
    runtime.html</a>

コード
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

driver = webdriver.Chrome('C:\\Dev\\tool\\webdriver\\chromedriver.exe')  
driver.get('http://xxx.xxx.xxx.xxx:8088/')
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:file://var/export/web/fess/pgsql11/app-pgrestore.html
    # ~
    # 19:file://var/export/web/fess/pgsql11/multibyte.html

    time.sleep(2) # 表示確認用のwait
finally:
    driver.quit()
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