Home > Software design >  Selenium chromedriver for retrieving current value of dropdown table <tbody> data from a Webpa
Selenium chromedriver for retrieving current value of dropdown table <tbody> data from a Webpa

Time:12-15

table structure for web Url is below.

<body>
    <div >
        <select >
            <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 >
        <span >
        <thead>
            <tr >
                <th ><span >Exchange</span><!----></th>
                <th ><span >Calls</span><!----></th>
                <th ><span >Puts</span><!----></th>
                <th ><span >P/C Ratio</span><!----></th>
                <th ><span >Volume</span><!----></th>
                <th ><span >Market Share</span><!----></th>
            </tr>
        </thead></span>
        <tbody>
            <tr >
                <td >AMEX</td>
                <td >1,397,381</td>
                <td >1,261,149</td>
                <td >0.9</td>
                <td >2,658,530</td>
                <td >6.73%</td>
            </tr>
        </tbody><!----><!----><!---->
    </table>
</body>

Code which i am trying get only first option Total Options data. other two option value of dropdown current <tbody> data is not getting. In every dropdown menu <tbody> data is different, table structure is same for all three menu only data is different. can you please help me to amended the code for get current value of dropdown menu Thanks in advance!

   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]);
                    }
                }
            }

CodePudding user response:

You should look into HTMLAglityPack. 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']");. Then you can run a for loop on the market data: foreach(var data in marketData){string x = data.innerText;}

CodePudding user response:

The below code is in Java, modify this code to C#, but you can use these same locators:

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% | 
...
...
  • Related