Python 将值提交到网页的组合框和Datetime对象后获取数据

Python 将值提交到网页的组合框和Datetime对象后获取数据,python,python-3.x,pandas,selenium,selenium-chromedriver,Python,Python 3.x,Pandas,Selenium,Selenium Chromedriver,我想从链接中获取值,所以编写代码直到打开页面 import os from webdriver_manager.chrome import ChromeDriverManager import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.keys import Keys from selenium.w

我想从链接中获取值,所以编写代码直到打开页面

import os
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
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
options = Options()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--start-maximized')
options.page_load_strategy = 'eager'
options.add_argument("--headless");
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 20)   
driver.get("https://www1.nseindia.com/products/content/derivatives/equities/historical_fo.htm")
在此之后,需要输入值

比如说,

1. Select Instrument = "Option Types"
2. Select Symbol = "Reliance"
3. Select Year = "2020"
4. Select Expiry = "31-Mar-2016"
5. Select Option Type = "CE"
6. Strike Price = 960
7. For past = 24 Months
8. From Period = "22-Feb-2016"
9. To Expiry = "31-Mar-2016"
然后单击“获取数据”按钮,我们得到一个如下所示的表


这需要在数据框中捕获所有数据,并且需要逐个打印关闭列中的所有值。如何在框中输入数据并获取此数据?

对于输入数据,您有下拉列表和文本框WebElements。您可以将数据插入其中,如本例所示:

from selenium.webdriver.support.ui import Select

options_from_instrument = Select(driver.find_element_by_xpath("//form[@name = 'dataForm']/div[@class = 'viewdata-content']/select"))
options_from_instrument.select_by_visible_text("Stock Options")

strike_price = driver.find_element_by_xpath("//form[@name = 'dataForm']/div[@class = 'viewdata-content']/input[@name = 'strikePrice']")
strike_price.send_keys("960")
要将所有数据捕获到一个数据帧中,可以将所有内容写入一个文件,并在列之间进行相应的分隔。对于打印列close,我认为仅使用检查列索引的整数就足够了

with open("data_table.txt", "r") as output:
entries = driver.find_element_by_xpath("//div[@class ='tabular-data-historic']//tbody/tr") #tr holds for each entry of the data frame.
for entry in entries:
    if entry.text != "Historical Contract-wise Price Volume Data": #We don't want the header.
        values_of_entry = driver.find_elements_by_xpath("./th")
        col = 0
        for value in values_of_entry:
            output.write(f"{value.text}\t")
            if col == 8:                        # Column Close is in index 8
                print (f"{value.text}\n")
            col+= 1

        output.write("\n")
output.close()

entries=driver.find_element_by_xpath(“//div[@class='tablar-data-historical']//tbody/tr”)#tr对数据帧的每个条目都有效。-这是一个错误