我想從 https 響應(yīng)中獲取數(shù)據(jù),但它們的數(shù)據(jù)只有在單擊打開對話框窗口的按鈕后才會(huì)出現(xiàn)(查看下面的屏幕),所以我必須打開所有必要的窗口,然后運(yùn)行 request.get。主要困難是我不明白如何為我運(yùn)行 driver.get 命令的同一窗口制作 request.get 。import requestsfrom selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECchrome_options = webdriver.ChromeOptions()chrome_options.add_argument("start-maximized")driver = webdriver.Chrome(options=chrome_options)driver.get(url)XPATH = "//*[name()='svg' and contains(@class, 'jss350')]"XPATH2 = '/html/body/div[3]/div[3]/div/div[3]/button/span[1]'# Wait for all svg elements to be clickableWebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, XPATH)))# find list of svg elementssvg_elements = driver.find_elements_by_xpath(XPATH)for element in svg_elements[3:4]: element.click() #open windows WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, XPATH2))).click() #close#here is I want to run the request.get function to grab data from the same windowdriver.quit()params = { 'products': 'RU',}url = 'my url'r = requests.get(url, params=params, headers={ 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'})json_data = r.json()print(r.url)print(r.text)
1 回答

MM們
TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊
api 可以獨(dú)立于 UI 操作(硒自動(dòng)化)工作。這意味著您可以在 POST 或 Get 中傳遞必要的數(shù)據(jù),并且 api 響應(yīng)可用。因此,無需打開特定的窗口/對話框,您可以嘗試在 POSTMAN 客戶端/也在您的代碼中傳遞 api 數(shù)據(jù)。
添加回答
舉報(bào)
0/150
提交
取消