C# With Selenium – Doubts about xPath

Question:

I'm automating a page function and when it will click on the error image, I've tried several things, my question is, is the xPath I'm sending correct?

<div id="toolbar">
        <table cellspacing="1px" cellpadding="1px" width="100%" class="birtviewer_toolbar">
            <tbody><tr><td></td></tr>
            <tr>
                <td width="6px">
                </td><td width="15px">
                   <input type="image" name="toc" src="birt/images/Toc.gif" title="Comutar índice" alt="Comutar índice" class="birtviewer_clickable">
                </td>
                <!--
                <TD WIDTH="6px"/>
                <TD WIDTH="15px">
                   <INPUT TYPE="image" NAME='parameter' SRC="birt/images/Report_parameters.gif"
                        TITLE="Executar relatório"  
                        ALT="Executar relatório" CLASS="birtviewer_clickable">
                </TD>
                <TD WIDTH="6px"/>
                <TD WIDTH="15px">
                   <INPUT TYPE="image" NAME='export' SRC="birt/images/Export.gif"
                        TITLE="Exportar dados"
                        ALT="Exportar dados" CLASS="birtviewer_clickable">
                </TD>
                -->
                <td width="6px">
                </td><td width="15px">
                   <input type="image" name="exportReport" src="birt/images/ExportReport.gif" title="Exportar relatório" alt="Exportar relatório" class="birtviewer_clickable">
                </td>
                <td width="6px">
                </td><td width="15px">
                   <input type="image" name="print" src="birt/images/Print.gif" title="Imprimir relatório" alt="Imprimir relatório" class="birtviewer_clickable">
                </td>

                <td align="right">
                </td>
                <td width="6px">
            </td></tr>
        </tbody></table>
    </div>

Class I need to click

  </td><td width="15px">
               <input type="image" name="exportReport" src="birt/images/ExportReport.gif" title="Exportar relatório" alt="Exportar relatório" class="birtviewer_clickable">
            </td>

xPath driver.FindElement(By.XPath("//a[./img[@name='exportReport']tr[2]/td[4]/]")).Click();

Answer:

Certainly your XPath is not right and is returning null.

One way you can do it is as follows.

var inputs = browser.FindElementsByTagName("input");
foreach (var item in inputs)
{
    var alt = item.GetAttribute("src");
    if (alt != null && alt.Contains("Exportar relatório"))
    {
        item.Click();
        break;
    }
}
Scroll to Top
AllEscort