html Selenium chromedriver用于从网页中检索下拉表数据的当前值< tbody>并导出为csv文件

mmvthczy  于 2022-12-16  发布在  其他
关注(0)|答案(2)|浏览(124)

Web URL表结构如下。

<body>
    <div class="marketData-inputSelect">
        <select class="js-select js-optionsDataFilter">
            <option value="total_volume">Total Options</option>
            <option value="equity_volume">Equity Options</option>
            <option value="index_volume">Index/Other Options</option>
        </select>
    </div>
    <table class="marketData-table marketData-table--active marketData-table--border marketData-table--padded marketData-table--auto table-responsive">
        <span class="table-head__cond">
        <thead>
            <tr class="marketData-tableRow">
                <th class="marketData-tableHeading"><span class="text">Exchange</span><!----></th>
                <th class="marketData-tableHeading"><span class="text">Calls</span><!----></th>
                <th class="marketData-tableHeading"><span class="text">Puts</span><!----></th>
                <th class="marketData-tableHeading"><span class="text">P/C Ratio</span><!----></th>
                <th class="marketData-tableHeading"><span class="text">Volume</span><!----></th>
                <th class="marketData-tableHeading"><span class="text">Market Share</span><!----></th>
            </tr>
        </thead></span>
        <tbody>
            <tr class="marketData-tableRow marketData-tableRow--fill">
                <td class="marketData-tableItem">AMEX</td>
                <td class="marketData-tableItem">1,397,381</td>
                <td class="marketData-tableItem">1,261,149</td>
                <td class="marketData-tableItem">0.9</td>
                <td class="marketData-tableItem">2,658,530</td>
                <td class="marketData-tableItem">6.73%</td>
            </tr>
        </tbody><!----><!----><!---->
    </table>
</body>

代码,我正在尝试只得到第一个选项Total Options数据。其他两个选项值dropdown当前<tbody>数据没有得到。在每个下拉菜单<tbody>数据是不同的,表结构是相同的所有三个菜单只有数据是不同的。你能帮我修改代码get current value of dropdown menu提前感谢!

var dropdownText = driver.FindElement(By.XPath("//select"));
   var dropdownSelect = new SelectElement(dropdownText);
   var dropdownSelectvalue = dropdownSelect.SelectedOption.GetAttribute("value");
   if (driver.FindElement(By.XPath("//table/span/thead/tr/th")).Displayed)
            {
                IWebElement webElementHead = driver.FindElement(By.XPath("//table/span/thead/tr"));
                IList<IWebElement> ElementCollectionHead = webElementHead.FindElements(By.XPath("//table/span/thead/tr/th"));
                foreach (IWebElement item in ElementCollectionHead)
                {
                    Console.WriteLine(item.Text);
                }
            }
            if (driver.FindElement(By.XPath("//table/tbody/tr")).Displayed)
            {
                IWebElement webElementBody = driver.FindElement(By.XPath("//table/tbody/tr"));
                IList<IWebElement> ElementCollectionBody = webElementBody.FindElements(By.XPath("//table/tbody/tr"));
                foreach (IWebElement item in ElementCollectionBody)
                {
                    string[] arr = new string[4];
                    arr = item.Text.Split(' ');
                    for (int i = 0; i < arr.Length; i++)
                    {
                        Console.WriteLine(arr[i]);
                    }
                }
            }

我正在尝试显示所有数据在ConsoleTotal Options成功。你能请store the data.csv文件我正在搜索我们可以做的使用FileStream,但不能成功。

FileStream fs =new FileStream((".csv"),FileMode.Create);
StreamWriter sw=new StreamWriter(fs);
sw.WriteLine();
sw.Close();
sw.Dispose();

不确定以上FileStream代码。

i2byvkas

i2byvkas1#

你应该研究一下HTMLAglityPack。使用HTMLAglityPack,我会将驱动程序.Pagesouce加载到一个新的HTML文档中,然后执行以下操作:var marketData = html.DocumentNode.SelectNodes("//td[@marketData-tableItem']");。然后可以对市场数据运行for循环:foreach(var data in marketData){string x = data.innerText;}

c7rzv4ha

c7rzv4ha2#

下面的代码是用Java编写的,请将此代码修改为C#,但您可以使用这些相同的定位器:

driver.get("<URL>");

// to accept cookies        
driver.findElement(By.xpath(".//*[text()='Accept analytic cookies']")).click();
        
// to click on the view button      
driver.findElement(By.cssSelector(".marketData-inputBtn")).click();
        
// to print the headings        
List<WebElement> headings = driver.findElements(By.xpath("(.//*[@class='marketData-resultsSection'])[2]//th/span"));
for (WebElement ele :  headings) {
    System.out.print(ele.getText() + " | ");
}
System.out.println();
        
// to print the data        
List<WebElement> data = driver.findElements(By.xpath("(.//*[@class='marketData-resultsSection'])[2]//td"));
int i = 0;
for (WebElement ele : data) {
    i++;
    System.out.print(ele.getText() + " | ");
    if (i == 6) {
        i = 0;
        System.out.println();
    }
}

输出:

Exchange | Calls | Puts | P/C Ratio | Volume | Market Share | 
AMEX | 1,829,955 | 1,645,962 | 0.9 | 3,475,917 | 7.23% | 
...
...

相关问题