2023-04-20

Getting row and column name of a fillable box in Selenium Basic using FindElementbyXpath()

I'm using Selenium Basic to fill in text to a website from my spreadsheet. The html code of the website is something like this

<div>
    <table cellspacing="0" rules="all" border="1" id="ctl00_ContentPlaceHolder1_GridView1" style="width:99%;border-collapse:collapse;">
        <tr>
            <th scope="col" style="font-weight:bold;">FX</th>
            <th scope="col" style="color:White;">Pic</th>
            <th scope="col">Cash</th>
            <th scope="col">Bid</th>
            <th scope="col">Ask</th>
        </tr>
        <tr>
            <td style="font-weight:bold;">
                <span id="ctl00_ContentPlaceHolder1_GridView1_ctl02_Label1">USD1</span>
            </td>
            <td> &nbsp; <img id="ctl00_ContentPlaceHolder1_GridView1_ctl02_Image1" src="UploadFiles/QLTG-File/MH_200931393922.jpg" style="border-width:0px;" />
            </td>
            <td align="right"> &nbsp; <input name="ctl00$ContentPlaceHolder1$GridView1$ctl02$txtCash" type="text" value="4" id="ctl00_ContentPlaceHolder1_GridView1_ctl02_txtCash" style="width:100px;" />
            </td>
            <td align="right"> &nbsp; <input name="ctl00$ContentPlaceHolder1$GridView1$ctl02$txtBid" type="text" value="5" id="ctl00_ContentPlaceHolder1_GridView1_ctl02_txtBid" style="width:100px;" />
            </td>
            <td align="right"> &nbsp; <input name="ctl00$ContentPlaceHolder1$GridView1$ctl02$txtAsk" type="text" value="6" id="ctl00_ContentPlaceHolder1_GridView1_ctl02_txtAsk" style="width:100px;" />
            </td>
        </tr>
        <tr>
            <td style="font-weight:bold;">
                <span id="ctl00_ContentPlaceHolder1_GridView1_ctl03_Label1">USD2</span>
            </td>
            <td> &nbsp; <img id="ctl00_ContentPlaceHolder1_GridView1_ctl03_Image1" src="UploadFiles/QLTG-File/MH_20093139406.jpg" style="border-width:0px;" />
            </td>
            <td align="right"> &nbsp; <input name="ctl00$ContentPlaceHolder1$GridView1$ctl03$txtCash" type="text" value="7" id="ctl00_ContentPlaceHolder1_GridView1_ctl03_txtCash" style="width:100px;" />
            </td>
            <td align="right"> &nbsp; <input name="ctl00$ContentPlaceHolder1$GridView1$ctl03$txtBid" type="text" id="ctl00_ContentPlaceHolder1_GridView1_ctl03_txtBid" style="width:100px;" />
            </td>
            <td align="right"> &nbsp; <input name="ctl00$ContentPlaceHolder1$GridView1$ctl03$txtAsk" type="text" id="ctl00_ContentPlaceHolder1_GridView1_ctl03_txtAsk" style="width:100px;" />
            </td>
        </tr>
        <tr>
            <td style="font-weight:bold;">
                <span id="ctl00_ContentPlaceHolder1_GridView1_ctl04_Label1">USD3</span>
            </td>
            <td> &nbsp; <img id="ctl00_ContentPlaceHolder1_GridView1_ctl04_Image1" src="UploadFiles/QLTG-File/MH_201441583814.jpg" style="border-width:0px;" />
            </td>
            <td align="right"> &nbsp; <input name="ctl00$ContentPlaceHolder1$GridView1$ctl04$txtCash" type="text" value="    " id="ctl00_ContentPlaceHolder1_GridView1_ctl04_txtCash" style="width:100px;" />
            </td>
            <td align="right"> &nbsp; <input name="ctl00$ContentPlaceHolder1$GridView1$ctl04$txtBid" type="text" id="ctl00_ContentPlaceHolder1_GridView1_ctl04_txtBid" style="width:100px;" />
            </td>
            <td align="right"> &nbsp; <input name="ctl00$ContentPlaceHolder1$GridView1$ctl04$txtAsk" type="text" id="ctl00_ContentPlaceHolder1_GridView1_ctl04_txtAsk" style="width:100px;" />
            </td>
        </tr>
    </table>
</div>

I want to find the row and column name of a fillable box, given this element is got by fillableBoxes = driver.FindElementsByXPath("//input[@type='text']").

I try to get a specific row and column of a fillable box, for example

Table = driver.FindElementById("ctl00_ContentPlaceHolder1_GridView1")
Example = Table.FindElementById("ctl00_ContentPlaceHolder1_GridView1_ctl03_txtBid")
Dim rowElement As WebElement
Set rowElement = Example.FindElementByXPath("./ancestor::tr")
Dim columnHeaderElement As WebElement
Set columnHeaderElement = Example.FindElementByXPath("./ancestor::table/descendant::tr[1]/th[position() = count(./preceding-sibling::td)+1]")

Dim rowName As String
rowName = rowElement.FindElementByXPath("./th").Text
    
Dim columnName As String
columnName = columnHeaderElement.Text

The error is

NoSuchElementError - Element not found for Xpath=./th

and the debug stops at

rowName = rowElement.FindElementByXPath("./th").Text

It seems I got the XPath wrong. How can I get the right Xpath for getting row and column name?



No comments:

Post a Comment