使用Selenium和Python从网站的下拉列表中选择项目

anauzrmj  于 2023-05-16  发布在  Python
关注(0)|答案(1)|浏览(150)

我想问一下如何使用Selenium和Python在下拉列表中自动选择包含EUR的项目。该网站的参考资料在这里:
https://www.nasdaqomxnordic.com/shares/historicalprices
需要在搜索字段中键入子字符串,然后出现下拉列表。
我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import csv
import time
import glob
import os, fnmatch
import pandas as pd
import re, os.path
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.select import Select
from selenium.webdriver.firefox.options import Options as FirefoxOptions
#from msedge.selenium_tools import EdgeOptions
#from msedge.selenium_tools import Edge

print("Starting stock script")
p="C:/Users/apskaita3/Finansų analizės ir valdymo sprendimai, UAB/Rokas Toomsalu - Power BI analitika/Integracijos/1_Public comapnies analytics/2. Nasdaq Helsinki/Nasdaq_historical_prices"

stock_list =[

    {"name": "Nordea Bank Ab", "csv_name": "C:/Users/apskaita3/Finansų analizės ir valdymo sprendimai, UAB/Rokas Toomsalu - Power BI analitika/Integracijos/1_Public comapnies analytics/Databasesets/Nasdaq_historical_prices/NDA-FI.csv"}

 ]


for i in stock_list:
    csv_name = i['csv_name']
    print(f"Starting iteration {i}")
    cname = i['name']

    #chromeOptions = webdriver.ChromeOptions()
   
    #chromeOptions.add_argument('--remote-debugging-port=9230')
    
        
    #driver=webdriver.Firefox(executable_path=r'C:\Users\apskaita3\Desktop\gec\geckodriver.exe',service_log_path=os.devnull)
    #driver = webdriver.Edge(r"C:/Users/apskaita3/Desktop/msedgedriver.exe")

    """
    edge_options = EdgeOptions()
    edge_options.use_chromium = True
    edge_options.add_argument('headless')
    edge_options.add_argument("disable-gpu")
    edge_options.add_argument("--remote-debugging-port=9222")
    edge_options.add_argument("--no-sandbox")
    edge_options.add_argument("--disable-dev-shm-usage")
    """
    #driver = webdriver.Edge(r"C:/Users/apskaita3/Desktop/msedgedriver.exe",{"edge_options":"edge_options"})

    #driver = webdriver.Edge(r"C:/Users/apskaita3/Desktop/msedgedriver.exe",{"edge_options": edge_options})

    #driver = webdriver.Edge(r"C:/Users/apskaita3/Desktop/msedgedriver.exe", options=edge_options)

    driver = webdriver.Firefox(executable_path=r'C:/Users/apskaita3/Documents/geckodriver.exe', service_log_path="C:/Users/apskaita3/Downloads/geckodriver.log")
    if cname=="TietoEVRY Oy":


        #driver = webdriver.Edge(ChromeDriverManager().install())
        #driver = webdriver.Chrome("C:/Users/apskaita3/Desktop/chromedriver.exe")
        driver.maximize_window()
        driver.get("http://www.nasdaqomxnordic.com/shares/historicalprices")
        driver.refresh()
        fromDate = driver.find_element_by_id("FromDate")
        fromDate.click()
        fromDate.clear()
        fromDate.send_keys("2006-01-01")
        time.sleep(4)
        inputElement = driver.find_element_by_id("instSearchHistorical")
        inputElement.send_keys(cname)

        #driver.implicitly_wait(6)
        time.sleep(4)
        #s=driver.find_element_by_xpath("/html/body/ul[3]/li[3]/a/div[3]")
        #s=driver.find_elements_by_xpath("//*[contains(text(), 'EUR')]")
        #s=driver.find_elements_by_xpath("//div[contains(text(),'EUR')]")
        s=driver.find_elements_by_xpath("//*[matches(.,'EUR', 'i')]")
        #element = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Nordea Bank Abp (EUR)']")))
        driver.execute_script("arguments[1].click();", s)
        #.click()
        #inputElement.send_keys(Keys.ENTER)

        #driver.find_element_by_css_selector('div.acSearchResult-fnm').click()

        new_csv1 = pd.read_csv(csv_name,sep=';')
        if ('sep='  in new_csv1.columns.tolist()):
            new_csv1= pd.read_csv(csv_name,sep=';',skiprows=1)
        else:
            new_csv1= pd.read_csv(csv_name,sep=',')

        new_df1 = pd.concat([old_csv1, new_csv1]).reset_index(drop=True)
        #old_csv1=new_csv1
        new_df1.columns = old_csv1.columns
        new_df1['Trades']=new_df1['Trades'].replace(" ", "")
        new_df1=new_df1.sort_values('Date', ascending = False).drop_duplicates()
        if 'Unnamed: 11' in new_df1.columns.tolist():
            del new_df1['Unnamed: 11']
        else:
            pass
        new_df1.to_csv(csv_name,index = False)
        print(new_df1.duplicated())

        os.chdir("C:/Users/apskaita3/Downloads")
        for e in os.listdir():
          if "-2006-01-01" in e:
            os.remove(e)
          else:
            pass
                

        
        filenames.clear()
        time.sleep(3)        time.sleep(2)

        driver.find_element_by_id("showAdjusted").click()

                        
        time.sleep(4)
        driver.find_element_by_id("exportExcel").click()
        print(5)
        time.sleep(8)
        
        driver.close()

        time.sleep(3)
        print(5)

        #path = "C:/Users/apskaita3/Downloads/"
        accepted_extensions = ["csv"]
        filenames = [fn for fn in os.listdir("C:/Users/apskaita3/Downloads/") if fn.split(".")[-1] in accepted_extensions]
        #filenames = sorted(filenames, key=lambda t: -os.stat(t).st_mtime)
        #print(filenames)
        #filenames=re.search("2006",filenames) 
        #print(filenames)
         
        filenames = [s for s in filenames if "-2006-01-01" in s]

        print(os.listdir("C:/Users/apskaita3/Downloads/"))
        
        print(filenames)
        a1=filenames[0]

        try:
            old_csv1 = pd.read_csv(f'C:/Users/apskaita3/Downloads/{a1}',sep=';',skiprows=1)
        except pd.errors.EmptyDataError:
          #old_csv14 = pd.DataFrame()
            old_csv1  = pd.DataFrame(columns=['Date', 'Bid', 'Ask', 'Opening price','High price','Low price','Closing price', 'Average price','Total volume','Turnover','Trades'])

        new_csv1 = pd.read_csv(csv_name,sep=';')
        if ('sep='  in new_csv1.columns.tolist()):
            new_csv1= pd.read_csv(csv_name,sep=';',skiprows=1)
        else:
            new_csv1= pd.read_csv(csv_name,sep=',')

        new_df1 = pd.concat([old_csv1, new_csv1]).reset_index(drop=True)
        #old_csv1=new_csv1
        new_df1.columns = old_csv1.columns
        new_df1['Trades']=new_df1['Trades'].replace(" ", "")
        new_df1=new_df1.sort_values('Date', ascending = False).drop_duplicates()
        if 'Unnamed: 11' in new_df1.columns.tolist():
            del new_df1['Unnamed: 11']
        else:
            pass
        new_df1.to_csv(csv_name,index = False)
        print(new_df1.duplicated())

        os.chdir("C:/Users/apskaita3/Downloads")
        for e in os.listdir():
          if "-2006-01-01" in e:
            os.remove(e)
          else:
            pass
                

        
        filenames.clear()
        time.sleep(3)

bzzcjhmw

bzzcjhmw1#

试试下面的代码:

driver = webdriver.Chrome()
driver.get("https://www.nasdaqomxnordic.com/shares/historicalprices")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "nordicSearchBox"))).send_keys("Nordea Bank Abp")
# Below line locates the desired element and stores in the variable
element = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Nordea Bank Abp (EUR)']")))
# Below 2 lines will click on the desired element
action = ActionChains(driver)
action.move_to_element(element).click().perform()

所需进口:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains

相关问题