C# Selenium WebDriver - 如果元素存在选择它,如果没有忽略它并继续下一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18717878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:00:43  来源:igfitidea点击:

Selenium WebDriver - If element present select it, if not ignore it and continue to next element

c#seleniumwebdriverselenium-webdriver

提问by user2464219

I am just finishing a test script and accessing a fairly dynamic page. The page in question, has an element appear (generally a radio button or tick-box), which is only present if certain criteria in previous pages are met. So, my test will be accessing this page irrelevant of previous criteria and I want to hit the "continue" element at the bottom of the page whilst handling these elements "IF" they appear. I have a few method sto click elements by ID, and so far have the following code:

我刚刚完成了一个测试脚本并访问了一个相当动态的页面。有问题的页面有一个元素出现(通常是一个单选按钮或勾选框),只有在满足之前页面中的某些条件时才会出现。因此,我的测试将访问与先前标准无关的此页面,并且我想在处理这些元素“如果”它们出现的同时点击页面底部的“继续”元素。我有一些方法可以通过 ID 单击元素,到目前为止有以下代码:

 // Selects the "Confirm" button
                IWebElement radioOption = mWebDriver.FindElement(By.Id("Radio_Button_Id"));
                if (radioOption.Displayed)
                {
                    this.ClickElementById("Radio_Button_Id");

                    // Clicks CONTINUE
                    this.ClickElementById("CONTINUE");
                }
                else
                {
                    // Selects CONTINUE
                    this.ClickElementById("CONTINUE");
                }

I am trying in this code to handle that if the radio button appears, select it then select the continue button. Also, if the radio button does not appear, ignore it and select the continue button. Any help with this would be much appreciated.

我试图在这段代码中处理如果单选按钮出现,选择它然后选择继续按钮。此外,如果单选按钮未出现,请忽略它并选择继续按钮。对此的任何帮助将不胜感激。

采纳答案by Tedesco

Try something like this:

尝试这样的事情:

//Displayed
public static bool IsElementDisplayed(this IWebDriver driver, By element)
{
    IReadOnlyCollection<IWebElement> elements = driver.FindElements(element);
    if (elements.Count > 0)
    {
        return elements.ElementAt(0).Displayed;
    }
    return false;
}

//Enabled
public static bool IsElementEnabled(this IWebDriver driver, By element)
{
    IReadOnlyCollection<IWebElement> elements = driver.FindElements(element);
    if (elements.Count > 0)
    {
        return elements.ElementAt(0).Enabled;
    }
    return false;
}

You'll not get any exception and then the test can continue.

你不会得到任何异常,然后测试可以继续。

回答by Nathan Merrill

You said that you were getting NoSuchElementExceptions. radioOption.Displayedtests to see if the element is visible on the page, but it will throw an error if the element doesn't even exist. (An element can be present, but invisible)

你说你得到了 NoSuchElementExceptions。radioOption.Displayed测试元素是否在页面上可见,但如果元素不存在,它将抛出错误。(元素可以存在,但不可见)

To test to see if an element is present, you need to do mWebDriver.FindElements(note the S). This will return a List<WebElement>of all of the elements that match your selector, and if it can't find any, it will return a list of size 0 (and not throw an error).

要测试元素是否存在,您需要执行mWebDriver.FindElements(注意 S)。这将返回List<WebElement>与您的选择器匹配的所有元素中的一个,如果找不到任何元素,它将返回一个大小为 0 的列表(并且不会抛出错误)。

That way, your if statement will be if (radioOptions.size()!=0), and will check to see if the element exists (not if its visible).

这样,您的 if 语句将是if (radioOptions.size()!=0), 并将检查该元素是否存在(而不是它是否可见)。

回答by Greg Burghardt

I've also used this as a way to test if the element is present and get a handle on the element if it is present:

我还使用它来测试元素是否存在并获取元素的句柄(如果存在):

namespace SeleniumExtensions
{
    public static class WebDriverExtensions
    {
        public static bool TryFindElement(this IWebDriver driver, By by, out IWebElement element)
        {
            try
            {
                element = driver.FindElement(by);

                return true;
            }
            catch (NoSuchElementException)
            {
                element = null;

                return false;
            }
        }

        public static bool IsElementEnabled(this IWebDriver driver, By by)
        {
            IWebElement element = null;

            if (driver.TryFindElement(by, out element))
            {
                return element.Enabled;
            }

            return false;
        }
    }
}

This allows for code like:

这允许代码如下:

using SeleniumExtensions;

// ...

IWebElement element = null;

if (driver.TryFindElement(By.Id("item-01"), out element)
{
    // use element
}
else
{
     // element is null
}

Or:

或者:

if (driver.IsElementEnabled(By.Id("item-01"))
{
    // item is enabled
}