Search code examples
javaseleniumselenium-webdriverselenium-chromedriverrelative-locators

How to perform click action on this particular button?


I was asked this question on interview. I need to perform click action on "open" button which is opposite to "CLR-9194":

enter image description here

I told that with help of index I can perform action on that particular "open" button:

findElement(By.xpath("//label[2]"))

But he told me that I was wrong.


Solution

  • You need to locate the table row parent element based on the CLR-9194 content and then to find the open button child element of that row.
    So, if each table row is td element (it can be tr, I don't know, you did not share the XML) and the open element is "label" the XPath can be something like this:

    "//td[contains(.,'CLR-9194')]//label"
    

    The Selenium code will be correspondingly something like:

    findElement(By.xpath("//td[contains(.,'CLR-9194')]//label"))
    

    the XPath above can and should be made more precise. We need to see the XML for that.