32. 常见元素定位 - 文件下载

2024-03-15  本文已影响0人  薛东弗斯

CNPM Binaries Mirror (npmmirror.com)

image.png
# project > CH7 > file_download2.py
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from utils.get_file_path import get_download_path

path = get_download_path() + "/LATEST_RELEASE"
if os.path.exists(path):
    print("文件存在")
    os.remove(path)
    print("文件已删除")

chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory": "{}".format(get_download_path())}
chromeOptions.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chromeOptions)
driver.maximize_window()
driver.get("https://registry.npmmirror.com/binary.html?path=chromedriver/")
time.sleep(3)
driver.find_element(By.XPATH, '/html/body/table/tbody/tr[156]/td[2]/a').click()
time.sleep(3)
driver.close()
# project > utils > get_file_path.py
import os

def get_logo_path():
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))),"file","logo.bmp")
    return path
# print(get_logo_path())

def get_download_path():
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))),"file")
    return path
上一篇 下一篇

猜你喜欢

热点阅读