我想從以下網站決議地址。https://filialen.migros.ch/de/center:46.8202,6.9575/zoom:8/
到目前為止,我能夠進入該網站并點擊離開任何彈出式視窗。但是,我需要選擇帶有 "1163 STANDORTE "的下拉選單,我無法用我的代碼定位。 到目前為止,我的代碼是:
import pandas as pd
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.support.ui import Select
from bs4 import BeautifulSoup
import time
import itertools
import os
import numpy as np
import csv
import pdb
os.chdir("Directory")
options = webdriver.ChromeOptions()
options.add_argument("-incognito")
driver = webdriver.Chrome('Directory/chromedriver.exe' )
driver.get("https://filialen.migros.ch/de/center:46.8202,6.9575/zoom:8/"/span>)
time.sleep(1)
try:
WebDriverWait(driver, 20).until(EC.Element_to_be_clickable((By. XPATH, "//*[@class='close-icon']")).click() # if有什么東西可以點擊離開
except:
通過
time.sleep(4)
然后我嘗試使用span和按鈕元素以及幾個導航選項:
#Version 1
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='sc-hKFxyN jdMjfs'])) .click()
#Version 2。
element = driver.find_element_by_class_name('sc-eCApnc kiXUNl sc-jSFjdj lcZmPE')
driver.execute_script("arguments[0].rollIntoView();", element)
driver.execute_script("arguments[0].click();", element)
# Version 3
element = driver.find_element_by_class_name('sc-eCApnc kiXUNl sc-jSFjdj lcZmPE')
driver.execute_script("arguments[0].click();", element)
#Version 4
WebDriverWait(driver, 20).until(EC. element_to_be_clickable((By.XPATH, "//*[@class='sc-eCApnc kiXUNl sc-jSFjdj lcZmPE']") ).click()
# Version 5
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div/main/nav/header/button[1]")).click()
# Version 6
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='1163 STANDORTE]")).click()
實際上,有三個問題:
- 如果我只是在我的Chrome瀏覽器上手動打開鏈接,會出現 "1163個STANDORTE",而如果我在Chrome瀏覽器上使用python打開鏈接,會出現較少的STANDORTE,但我不能放大。因此,我需要所有1163個STANDORTE出現。
- 我無法使用類和XPATH來定位該按鈕。
- 按鈕后面可能是一個鏈接的XML檔案,而地址的資訊只有在點擊按鈕后才會出現。最后,我想抓取寫在鏈接到該按鈕的 XML 檔案上的文本。
有什么建議嗎?
我的問題與之前的這些問題類似。如何在 python 中決議具有相同類名的網站的幾個屬性?以及 Selenium-Debugging。元素在(X,Y)點無法點擊
uj5u.com熱心網友回復:
你正在尋找的資料是基于fetch / xhr的呼叫。
你可以得到它不需要搜刮。見下文。
import requests
headers = {'Origin': 'https://filialen.migros.ch',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36' }}r = requests.get()
r = requests.get(
'https://web-api.migros.ch/widgets/stores? key=loh7Diephiengaiv&aggregation_options[empty_buckets]=true&filters[market][0][0]=super& filters[market][0][1]=mno&。 filters[markets][0][2]=voi&filters[markets][0][3]=mp&filters[markets][0][4]=out&filters[markets][0][5]=spx&filters[markets][0][6]=doi& filters[markets][0][7]=mec&filters[markets][0][8]=mica&filters[markets][0][9]=res&filters[markets][0][10]=flori&filters[markets][0][11]=gour& filters[markets][0][12]=alna&filters[markets][0][13]=cof&filters[markets][0][14]=chng&verbosity=store&offset=0&limit=5000',
headers=headers)
if r.status_code == 200:
print('存盤資料如下:')
data = r.json()
print(data)
else:
print(f'Oops. 狀態代碼是{r.status_code}')
uj5u.com熱心網友回復:
幾個要點 :
在全屏模式下啟動b瀏覽器。
使用明確的等待。
使用這個xpath //span[包含(@aria-label, 'Standorte anzeigen')]/...
示例代碼 :
driver = webdriver.Chrome(driver_path)
driver.maximum_window()
#driver.implicitly_wait(50)
wait = WebDriverWait(driver, 20)
driver.get("https://filialen.migros.ch/de/center:46.8202,6.9575/zoom:8/"/span>)
try:
WebDriverWait(driver, 20).until(EC. element_to_be_clickable((By. XPATH, "//*[@class='close-icon']")).click() # if there is smth to click away
例外:
通過
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[包含(@aria-label, 'Standorte anzeigen')]/."/span>)).click()。
匯入 :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/307707.html
標籤:
