Vish Shady
Vish Shady

Reputation: 327

Selenium Webdriver : How to select the dynamically generated button by taking the reference of previuos tag's xpath

Hi all i am facing difficulty in slelecting dynamically generated buttons (remove button). All Remove buttons have same ID. How can i click the remove button by taking xpath of Lync - user Access ?

i have 3 columns and 2 rows in the table. i want to click on button which is on second row.

I tried by xpath axes and my code looks like this : driver.findElement(By.xpath("//a[.='PC/E - Home Branch View ']/following-sibling::/td[3]/input")).click(); But it is throwing an error :

"org.openqa.selenium.InvalidSelectorException: The xpath expression '//a[.='PC/E - Home Branch View ']/following-sibling::/td[3]/input' cannot be evaluated or does notresult in a WebElement (WARNING: The server did not provide any stacktrace information)"

My HTML Code looks like this :

    <TR>
        <TD id="Lync - User Access" class="profileInnerTable">
            <A style="CURSOR: hand" class="TabLink" onmouseover="ddrivetip1('Default for CBA users not in RBS Branches','lightyellow',250)" onmouseout="hideddrivetip"() ;>Lync - User Access </A>
        </TD>
        <TD class="profileInnerTable" align="middle">&nbsp; </TD>
        <TD class="profileInnerTable">
            <INPUT style="WIDTH: 100px; HEIGHT: 20px" tabIndex="0" onclick="processRemoveRoleFromDN(form, 'erglobalid=7178977152403244193,ou=roles,erglobalid=00000000000000000000,ou=CBA,dc=com');" value="Remove" type="button" name="removeDiscRole"> 
        </TD>
    </TR>
    <TR>
        <TD id="PC/E - Home Branch View " class="profileInnerTable">
            <A style="CURSOR: hand" class="TabLink" onmouseover="ddrivetip1('PC/E access to user\'s Home Branch only - For RBS Branch Users','lightyellow',250)" onmouseout="hideddrivetip"() ;>PC/E - Home Branch View </A>
        </TD>
        <TD class="profileInnerTable" align="middle">&nbsp; </TD>
        <TD class="profileInnerTable">
            <INPUT style="WIDTH: 100px; HEIGHT: 20px" tabIndex="0" onclick="processRemoveRoleFromDN(form, 'erglobalid=3113620533928290009,ou=roles,erglobalid=00000000000000000000,ou=CBA,dc=com');" value="Remove" type="button" name="removeDiscRole"> 
        </TD>
    </TR>

I want to click on remove button (in the second row). Can anyone please suggest me how to click on remove button by taking reference by the ID or Xpath of elements in the same row?

PS : if i execute the code driver.findElement(By.xpath("//input[@name='removeDiscRole']")).click();

it will just clicks on 1st remove button (1st row).

Upvotes: 0

Views: 2054

Answers (2)

hr_117
hr_117

Reputation: 9627

Q.: I want to click on remove button (in the second row)

Try this for the "removeDiscRole" button in the second row:

//tr[2]//input[@name='removeDiscRole']"

Or "(//input[@name='removeDiscRole'])[2]"for the second "removeDiscRole" button at all

Q.: How to click on remove button by taking reference by the ID or Xpath of elements in the same row.

"//tr[td[@id = 'PC/E - Home Branch View ']]//input[@name='removeDiscRole']"

Upvotes: 1

Garreth Golding
Garreth Golding

Reputation: 1005

I think this xpath will work.

*

//td[@id = 'PC/E - Home Branch View ']//following-sibling::td//input

*

The 'a' tag which you started off using does not have any siblings. The 'td' tags are not on the same level. They are a level above as the 'a' tag is nested inside the 'td' tag

Upvotes: 2

Related Questions