Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
738 views
in Technique[技术] by (71.8m points)

Check if element is interactable in C# Selenium/ChromeDriver app?

I have a C# Selenium app that uses the ChromeDriver/WebDriver NuGet package that supports Chrome 88. My Chrome version is 88.0.4324.104.

Some of the web pages I am working with have more than one BUTTON element with the same class and and tagName so when I execute my FindDomElement() call using my desired xpath, I get back multiple matching elements. Only one of the returned elements is interactable and the other ones are not. Right now I try executing a "Click()" call on each element until I find the right one, catching the ElementNotInteractableException exception for the ones that fail.

Is there a way to ask Selenium if an element is interactable?

Please note, all the elements are visible and enabled and I already know how to check the status of those properties via C#. They simply are not helpful in this case.

question from:https://stackoverflow.com/questions/65892069/check-if-element-is-interactable-in-c-sharp-selenium-chromedriver-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An extension method might be a better solution here. It would return true if the element was clicked, and return false if the element was not interactable.

public static class WebElementExtensions
{
    public static void TryClick(this IWebElement element)
    {
        try
        {
            element.Click();

            return true;
        }
        catch (ElementNotInteractableException)
        {
            return false;
        }
    }
}

Then it is just a simple test:

if (button.TryClick())
{
    // Button was clicked successfully
}
else
{
    // Button is not interactable
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...