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]);
}
}
}
我正在尝试显示所有数据在Console
为Total 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
代码。
2条答案
按热度按时间i2byvkas1#
你应该研究一下HTMLAglityPack。使用HTMLAglityPack,我会将驱动程序.Pagesouce加载到一个新的HTML文档中,然后执行以下操作:
var marketData = html.DocumentNode.SelectNodes("//td[@marketData-tableItem']");
。然后可以对市场数据运行for循环:foreach(var data in marketData){string x = data.innerText;}
c7rzv4ha2#
下面的代码是用Java编写的,请将此代码修改为C#,但您可以使用这些相同的定位器:
输出: