基於selenium的動態網頁Xpath測試工具

最近在搞一些Xpath網頁規則的編寫,發現網上的Xpath測試工具很多,但都是基於靜態頁面的。暫時還沒有發現基於動態頁面的Xpath測試工具,爲了後續的測試方便,於是就自己動手寫了一個

from tkinter import *
import tkinter as tk
import re
from lxml import etree
from selenium import webdriver

# 創建chrome無頭瀏覽器
driver = ""
try:
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    driver = webdriver.Chrome(chrome_options=chrome_options)
except:
    fire_profile = webdriver.FirefoxOptions()
    fire_profile.add_argument('--disable-gpu')  # 設置無頭模式
    fire_profile.add_argument('-headless')  # 設置無頭模式
    driver = webdriver.Firefox(options=fire_profile)
# 動態網頁源碼字段
source = ""
# 輔助字段
old_url = ""


def result_to_string(result):
    """
    將匹配到的網頁內容輸出
    :param result: 已經匹配好的網頁內容
    :return:
    """
    if isinstance(result, list):
        out_str = ""
        out_html = ""
        for one in result:
            if isinstance(one, str):
                out_str = out_str + one + "\n"
                out_html = out_html + one + "\n"
            else:
                out_str = out_str + one.text + "\n"
                out_html = out_html + etree.tostring(one, pretty_print=True, encoding="utf-8").decode("utf-8") + "\n"
        return out_str.replace(" ", "").strip("\n"), out_html.replace(" ", "").strip("\n")
    elif isinstance(result, str):
        return result.replace(" ", ""), result.replace(" ", "")
    else:
        return "", ""


def jiazai():
    """
    加載動態網頁源碼
    :return:
    """
    global old_url, source
    url = url_text.get()
    if not (str(url).startswith("http://") or str(url).startswith("https://")):
        source_text.delete(1.0, 'end')
        source_text.insert("insert", "請檢查是否添加http或https前綴!!!")
        return None
    if not str(url).__contains__("."):
        source_text.delete(1.0, 'end')
        source_text.insert("insert", "請輸入正確格式的網址!!!")
        return None
    if url != "" and (source_text.get(1.0, 1.1) == "" or old_url != url):
        source_text.delete(1.0, 'end')
        old_url = url
        try:
            driver.get(url)
            source = driver.page_source
            if source == '<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>':
                source_text.insert("insert", "請輸入真實的網址!!!")
            else:
                source_text.insert("insert", source)
        except Exception as e:
            source_text.insert("insert", "此網址無法解析,請輸入其他的網址!!!")


def ceshi():
    """
    將匹配到的網頁結果顯示出來
    :return:
    """
    global source
    if source != "" and xpath_text.get() != "":
        html = etree.HTML(source)
        print(xpath_text.get())
        result = html.xpath(xpath_text.get())
        out_string, out_html = result_to_string(result)
        result_text.delete(1.0, "end")
        result_source_text.delete(1.0, "end")
        result_text.insert("insert", out_string)
        result_source_text.insert("insert", out_html)


window = tk.Tk()
window.title('動態網頁XPATH驗證工具    Designed by Mr.Li')
window.geometry('750x560')

# 框架列
url_frame = tk.Frame(window)
url_frame.pack()
xpath_frame = tk.Frame(window)
xpath_frame.pack()
result_frame = tk.Frame(window)
result_frame.pack()
html_frame = tk.Frame(window)
html_frame.pack()
source_frame = tk.Frame(window)
source_frame.pack()

# 待匹配網址輸入列
url_label = tk.Label(url_frame, text='請輸入網址:')
url_label.pack(side=LEFT)
url_text = tk.Entry(url_frame, show=None, width=45)
url_text.pack(side=LEFT)
button1var = StringVar()
button1var.set("加載網頁")
url_button = tk.Button(url_frame, textvariable=button1var, width=10, command=jiazai)
url_button.pack(side=RIGHT)

# xpath規則輸入列
xpath_label = tk.Label(xpath_frame, text='請輸入規則:')
xpath_label.pack(side=LEFT)
xpath_text = tk.Entry(xpath_frame, show=None, width=45)
xpath_text.pack(side=LEFT)
xpath_button = tk.Button(xpath_frame, text="測試", width=10, command=ceshi)
xpath_button.pack(side=RIGHT)

# 結果列
result_label = tk.Label(result_frame, text='結果文字:')
result_label.pack(side=LEFT)
result_text = tk.Text(result_frame, show=None, height=9, width=80)
result_text.pack(side=LEFT)
result_source_label = tk.Label(html_frame, text='結果源碼:')
result_source_label.pack(side=LEFT)
result_source_text = tk.Text(html_frame, show=None, height=9, width=80)
result_source_text.pack(side=LEFT)

# 源碼列
source_label = tk.Label(source_frame, text='網頁源碼:')
source_label.pack(side=LEFT)
source_text = tk.Text(source_frame, height=17, width=80)
source_text.pack(side=LEFT)
window.mainloop()

下圖爲啓動界面:
在這裏插入圖片描述
下圖爲運行結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章