【发布时间】:2021-11-11 06:36:51
【问题描述】:
我想从以下网站解析地址: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/")
time.sleep(1)
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@class='close-icon']"))).click() # if there is smth to click away
except:
pass
time.sleep(4)
然后我尝试使用 span 和 button 元素以及几个导航选项:
#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].scrollIntoView();", 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”,而如果我使用 python 在 Chrome 上打开链接,会出现较少的 STANDORTE,但我无法缩小。所以我非常需要 ALL 1163 STANDORTE 出现。
- 我无法使用类和 XPATH 找到按钮。
- 按钮后面是一个可能链接的XML文件,地址信息只有在单击按钮后才会出现。最后,我想抓取文本,写在链接到该按钮的 XML 文件上。
有什么建议吗?
我的问题与之前的问题类似:How to parse several attributes of website with same class name in python? 和Selenium-Debugging: Element is not clickable at point (X,Y)
【问题讨论】:
标签: python xml selenium xpath selenium-chromedriver