无法从 selenium python 的下拉列表中 select

Unable to select from dropdown in selenium python

python 和硒相对较新。尝试使用 selenium 库 运行 python 中的以下代码。使用 css 选择器不会激活下拉列表。我也不确定我是否使用正确的语法来选择日期。

如有任何帮助,我们将不胜感激。提前致谢。!!

from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

s=Service("C:\python\chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.maximize_window()
wait=WebDriverWait(driver,20)
driver.get('http://www.amfiindia.com/research-information/other-data/mf-scheme-performance-details')
time.sleep(2)

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#form-fund-details > div > div:nth-child(1) > div:nth-child(1) > div > div > button > span.bs-caret > span"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[@class='end-type']/a[.='Open-ended']"))).click()

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"##form-fund-details > div > div:nth-child(1) > div:nth-child(2) > div > div > button > span.bs-caret > span"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[@class='primary-category']/a[.='Debt']"))).click()

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#form-fund-details > div > div:nth-child(1) > div:nth-child(3) > div > div > button > span.bs-caret > span"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[@class='category']/a[.='Medium Duration']"))).click()

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#form-fund-details > div > div:nth-child(1) > div.col-sm-3.pdl0 > div > div > button > span.bs-caret > span"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[@class='amc']/a[.='All']"))).click()

datefield = driver.find_element_by_id('nav-date')
datefield.click()
datefield.send_keys("09-Mar-2022")

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.btn btn-primary amfi-btn'))).click()

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#download-report-excel > img"))).click()

driver.quit()

我不清楚是什么问题。根据您的描述,我假设您发现从日期选择器中选择日期时遇到问题?如果是,则日期选择器位于 Iframe 中。我们需要先切换到它。我们还需要在发送新数据之前清除输入字段的内容。下面是快速而肮脏的代码。希望这可以帮助。这是在 Java 中。从语法中,您可以轻松地替换 eq Python.

driver.get("https://www.amfiindia.com/research-information/other-data/mf-scheme-performance-details");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@src=' https://www.valueresearchonline.com/amfi/fund-performance']")));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#nav-date")));
driver.findElement(By.cssSelector("input#nav-date")).clear();
driver.findElement(By.cssSelector("input#nav-date")).sendKeys("07-Mar-2022");

我会在这里跳过使用 Selenium。唯一的问题是使用 cloudflare 检查了验证码。但是,有一个漂亮的包可以处理这个问题:

首先在您的环境中pip install cloudscraper

代码:

import pandas as pd
import cloudscraper

nav_date = '09-Mar-2022'
url = 'https://www.valueresearchonline.com/amfi/fund-performance-data/'
payload = {
    'end-type': '1',
    'primary-category': 'SDT',
    'category': 'SDT_MD',
    'amc': 'ALL',
    'nav-date': nav_date}

scraper = cloudscraper.CloudScraper()
res = scraper.get(url, params=payload).text

df = pd.read_html(res)[0]
df.to_csv(f'fund-performance-{nav_date}.csv')

输出:

print(df.to_string())
                                    Scheme                      Benchmark       Riskometer           Latest NAV (₹)  Previous NAV (₹)          1-Year Return (%)3-Year Return (%)5-Year Return (%)10-Year Return (%)Returns Since Launch (%)                                     Daily AUM (Cr.)
                                    Scheme                      Benchmark           Scheme Benchmark                          Regular   Direct                                                                                       Regular Direct Benchmark Unnamed: 9_level_1 Daily AUM (Cr.)
0   Aditya Birla Sun Life Medium Term Fund  CRISIL Medium Term Debt Index        Very High  Moderate                          26.2581  27.8779                                                                                          9.20   9.84      6.38                NaN         1543.26
1                 Axis Strategic Bond Fund    NIFTY Medium Duration Index  Moderately High  Moderate                          22.1686  23.8259                                                                                          5.56   6.31      6.19                NaN         1836.37
2             BNP Paribas Medium Term Fund  CRISIL Medium Term Debt Index         Moderate  Moderate                          15.3021  16.2018                                                                                          3.81   4.39      6.38                NaN           19.34
3                            DSP Bond Fund  CRISIL Medium Term Debt Index         Moderate  Moderate                          65.8516  69.1150                                                                                          4.41   4.80      6.38                NaN          356.60
4               HDFC Medium Term Debt Fund    NIFTY Medium Duration Index  Moderately High  Moderate                          45.4989  48.2688                                                                                          5.73   6.53      6.19                NaN         4094.24
5   ICICI Prudential Medium Term Bond Fund  CRISIL Medium Term Debt Index  Moderately High  Moderate                          35.5755  38.2344                                                                                          5.61   6.35      6.38                NaN         6886.97
6          IDFC Bond Fund Medium Term Plan    NIFTY Medium Duration Index         Moderate  Moderate                          37.8636  40.3042                                                                                          4.57   5.30      6.19                NaN         2564.22
7                   Kotak Medium Term Fund    NIFTY Medium Duration Index         Moderate  Moderate                          18.2767  19.6585                                                                                          6.03   7.12      6.19                NaN         2975.84
8            L&T Resurgent India Bond Fund    NIFTY Medium Duration Index         Moderate  Moderate                          16.4484  17.5125                                                                                          4.81   5.76      6.19                NaN         1037.95
9         Nippon India Strategic Debt Fund  CRISIL Medium Term Debt Index             High  Moderate                          12.5607  13.3599                                                                                         18.24  18.89      6.38                NaN          230.34
10         SBI Magnum Medium Duration Fund    NIFTY Medium Duration Index  Moderately High  Moderate                          40.9884  43.5373                                                                                          4.87   5.44      6.19                NaN        10426.94
11          Sundaram Medium Term Bond Fund    NIFTY Medium Duration Index         Moderate  Moderate                          58.8925  63.8394                                                                                          2.87   3.97      6.19                NaN           59.83
12                   Tata Medium Term Fund  CRISIL Medium Term Debt Index         Moderate  Moderate                          29.5529  32.3416                                                                                          5.57   6.55      6.38                NaN           83.34
13              Union Medium Duration Fund  CRISIL Medium Term Debt Index         Moderate  Moderate                          10.6048  10.6718                                                                                          4.82   5.21      6.38                NaN          219.25
14                    UTI Medium Term Fund  CRISIL Medium Term Debt Index         Moderate  Moderate                          14.9461  15.6951                                                                                          7.24   8.01      6.38                NaN           61.28