Search code examples
c#seleniumcucumbernunit-3.0

How can Write Web driver command object on background color


I want to capture New Expiry Date (image attached)

Following code I tried

wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//td[contains(@style,'background-color:rgb(164 164 187 / 15%);')]")));

return driver.FindElement(By.XPath("//td[contains(@style,'background-color:rgb(164 164 187 / 15%);')]"));
     

I got the element not the interactable error message. Please help me to resolve this. I selected my element on the page (Yellow color highlighted) in the attached image.

enter image description here


Solution

  • I would not recommend to have style attribute in your xpath. The reason is if the web developer change any color value

    background-color:rgb(164 164 187 / 15%);
    

    your xpath will fail. So we can say that it would be a brittle xpath.

    But Since you've just shared this HTML

    <td class="gridcell" style="background-color:rgb(164 164 187 / 15%);" aria-describedby="aaaca0c6-05dd-4b3b-b1ae-f79f6d42f64f" role="gridcell"></td>
    

    You can use the below xpath

    //td[@class='gridcell' and contains(@style,'background-color:rgb(164 164 187 / 15%);')]
    

    Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

    xpath that you should check :

    //td[@class='gridcell' and contains(@style,'background-color:rgb(164 164 187 / 15%);')]
    

    Steps to check:

    Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

    If we have 1/1 matching node, Please make sure that :

    1. This table/tr/td is not under an iframe.
    2. This table/tr/tdis not under a shadow-root.
    3. You should not be on new tab/windows launched by selenium.