一、上代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options as ChromeOptions
import time
def get_inventory_numbers_with_selenium(url, username, password):
# 设置 Chrome 选项
chrome_options = ChromeOptions()
chrome_options.add_argument("--headless") # 无头模式,不打开浏览器界面
#提前下载好与浏览器对应的驱动
chrome_service = ChromeService("F:\F\chrome\chromedriver-win64-v118\chromedriver.exe")
driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
try:
# 打开页面
driver.get(url)
time.sleep(5) # 等待页面加载,可以根据实际情况调整等待时间
# 找到在库存和在途数量的元素
stock_elem = driver.find_element(By.CSS_SELECTOR, 'div.con.sku-items span[data-v-4e5e55da=""]:nth-child(1)')
transit_elem = driver.find_element(By.CSS_SELECTOR,