[英]Selenium chromedriver for retrieving current value of dropdown table <tbody> data from a Webpage and export in csv file
table structure for web Url is below.网址的表结构如下。
<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>
Code which i am trying get only first option Total Options
data.我正在尝试的代码仅获取第一个选项
Total Options
数据。 other two option value of dropdown
current <tbody>
data is not getting. dropdown
当前<tbody>
数据的其他两个选项值未获取。 In every dropdown menu <tbody>
data is different, table structure is same for all three menu only data is different.在每个下拉菜单中,
<tbody>
数据不同,所有三个菜单的表结构相同,只是数据不同。 can you please help me to amended the code for get current value of dropdown menu
Thanks in advance!你能帮我修改代码以
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]);
}
}
}
I am trying to displaying all data in Console
for Total Options
successfully.我正在尝试在
Console
中成功显示所有数据以获取Total Options
。 can you please store the data
in .csv
file what I am searching we can do using FileStream
but not able succeed.您能否
store the data
在.csv
文件中,我正在搜索我们可以使用FileStream
执行但无法成功。
FileStream fs =new FileStream((".csv"),FileMode.Create);
StreamWriter sw=new StreamWriter(fs);
sw.WriteLine();
sw.Close();
sw.Dispose();
Not sure about above FileStream
Code.不确定上面的
FileStream
代码。
You should look into HTMLAglityPack.你应该看看 HTMLAgityPack。 With HTMLAglityPack I would load the driver.Pagesouce into a new HTML Document and then do something like:
var marketData = html.DocumentNode.SelectNodes("//td[@marketData-tableItem']");
使用 HTMLAgityPack,我会将 driver.Pagesouce 加载到一个新的 HTML 文档中,然后执行如下操作:
var marketData = html.DocumentNode.SelectNodes("//td[@marketData-tableItem']");
. . Then you can run a for loop on the market data:
foreach(var data in marketData){string x = data.innerText;}
然后您可以对市场数据运行 for 循环:
foreach(var data in marketData){string x = data.innerText;}
The below code is in Java, modify this code to C#, but you can use these same locators:下面的代码是用 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();
}
}
Output:输出:
Exchange | Calls | Puts | P/C Ratio | Volume | Market Share |
AMEX | 1,829,955 | 1,645,962 | 0.9 | 3,475,917 | 7.23% |
...
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.