Change the value in a cell of a table in Web Element using Selenium in C#

Viewed 180

I use this code to read each and every cell in the table from a webpage.

IList<IWebElement> all = driver.FindElements(By.ClassName("a-keyvalue"));
if (all.Count > 0)
{
    String[] allText = new String[all.Count];
    int i = 0;
    string test = "";
    foreach (IWebElement element in all)
    {
        //writeraa.WriteLine(element.Text);
        allText[i++] = element.Text;
        test = allText[i - 1];
    }

    string[] aa = test.Replace("\n", "").Split('\r');
    string JI = string.Join("", aa).Replace("\r\n", "").Replace("\r", "").Replace("\n", "");

enter image description here

Using this code I can read the 2 rows with values in it. I want to change the value "1" in the number row to 500.(Example - [Number | 1] to [Number | 500])

Can you please help me to know how can I access the number cell and set the value accordingly.

Thanks in advance.

HTML Code

<div class="content_view_list">
    <div>
        <h3></h3>
        <table class="a-keyvalue"> 
            <tbody>
                <tr>
                    <th>
                        <div class="tableHeader">
                            Item Name
                        </div>
                    </th>
                    <td>
                        <div>  
                            Wolike Powerful Aluminium Alloy Rubber Tubing Camouflage Bow Catapult Outdoor Hunting Fishing Slingshot
                        </div>
                    </td>
                </tr>
                <tr>
                    <th>
                        <div class="tableHeader">
                            Number
                        </div>
                    </th>
                    <td>
                        <div class="tableDetails">
                            3               
                        </div>
                    </td>
                </tr>
                <tr>
                    <th>
                        <div class="tableHeader">
                            Defect Body
                        </div>
                    </th>
                    <td>
                        <div>
                            CR-<span style="background-color:rgb(255,130,67)">DAMAGE</span>D_BY_CARRIER test
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
1 Answers

Since the "Number" cell is not a text input, you can have Selenium execute JavaScript, which will change the text in that cell.

var executor = (IJavaScriptExecutor)driver;
var number = 500;

executor.ExecuteScript($"arguments[0].innerHTML = '{number}';", element);
Related