试着使用XPath搜索和显示Elasticsearch插件网站上的内容

翻译目的

尝试使用Windows10 + Python3 + selenium + chromedriver + 无头 Chrome 来访问 Elasticsearch 插件的网站。
Fess 在维护或者新安装时,能否轻松进行 Elasticsearch 插件版本检查呢?
先创建一个临时的测试代码。
※规范尚未确定,插件已经固定,所以直接访问目标网址应该可以搞定。
将 org/codelibs 保存到本地并进行测试。

样本代码

# Windows Add env PYTHONIOENCODING = UTF-8 & restart vscode

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

# plugin のリスト
alist = [
    'elasticsearch-analysis-extension/',
    'elasticsearch-analysis-fess/',
    'elasticsearch-configsync/',
    'elasticsearch-dataformat/',
    'elasticsearch-minhash/'
]

# ブラウザーを起動
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('https://repo1.maven.org/maven2/org/codelibs/')
# 上記をローカルに保存
driver.get('http://localhost:8080/codelibs.html')

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.TAG_NAME, 'a'))
    )

    # xpath はこれで良いのだろうか?
    xpath = "/html/body/main/pre/a"
    elems = driver.find_elements_by_xpath(xpath)

    # listに存在する link text を表示する
    for elem in elems:
        if elem.text in alist:
            print(elem.text)

finally:
    print('done')
    driver.quit()

参考网站在这里。

在使用Python/Selenium中方便的xpath搜索时,重新使用find_element_by_xpath()方法搜索WebElement内部的困难。

bannerAds