簡體   English   中英

C# Selenium contains() 未找到正確的元素

[英]C# Selenium contains() not finding the correct element

出於某種原因,我不斷收到返回給我的錯誤文件。 我不斷收到一個名為 FC6 的文件...

我運行了調試器並且搜索變量一直保持 FC5,我猜它與 contains() function 有關,但我在其他任何地方都沒有遇到問題。

尋找解決此問題的方法,在此先感謝。

        public override void selectFiles()
        {
            foreach (string key in searchKeys)
            {
                IWebElement keyElement = base.getKeyElement("FC5");

                //get the parent of the key element
                IWebElement parentElement = keyElement.FindElement(By.XPath("./parent::*"));

                //get the download tag in the parent element
                IWebElement element = parentElement.FindElement(By.XPath("//*[contains(text(),'Download')]"));

                Actions actions = new Actions(driver);

                actions.MoveToElement(element).Click().Perform();
            }
        }

        public IWebElement getKeyElement(string searchKeys)
        {
            IWebElement element = null;

                string keyString = "//*[";
                string[] keyList = searchKeys.Split(",");
                bool firstKey = true;
                foreach (string searchKey in keyList)
                {
                    if (firstKey)
                    {
                        keyString = String.Join("", new string[] { keyString, "contains(text(), '", searchKey, "')" });
                    }
                    else
                    {
                        keyString = String.Join("", new string[] { keyString, " and contains(text(), '", searchKey, "')" });
                    }

                    firstKey = false;
                }
                keyString = String.Join("", new string[] { keyString, "]" });

                element = waitForVisibility(By.XPath(keyString));

            return element;
        }

        public IWebElement waitForVisibility(By element)
        {
            try
            {
                return new WebDriverWait(driver,        TimeSpan.FromSeconds(5)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(element));
            }
            catch
            {
                return null;
            }
        }

說真的,在將代碼復制粘貼到我的視覺工作室后,第一次知道OpenQA.Selenium

首先,這里是WebDriverWait.Until的定義:

public TResult Until<TResult>(Func<T, TResult> condition){}

我想知道你的ElementIsVisible是不是一個函數,但你能把它改成 Lambda 表達式嗎?

public IWebElement waitForVisibility(By element)
{
    return new WebDriverWait(driver, TimeSpan.FromSeconds(5))
    .Until(x=>SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(element)); //the lambda depends on your context.
}

以下是對您的代碼的一些建議:

public override void selectFiles()
{
    //should you run base.selectFiles() before or after the loop? base.selectFiles() has no any logic?

    foreach (string key in searchKeys)// key is never used inside loop, serious?
    {
        IWebElement keyElement = base.getKeyElement("FC5");
        //getKeyElement support multiple keys, why do you always get FC5 in each loop? 
        //should you move above line out if no need to getkey again

        IWebElement parentElement = keyElement.FindElement(By.XPath("./parent::*"));
        IWebElement element = parentElement.FindElement(By.XPath("//*[contains(text(),'Download')]"));//is this `Download` the `key`?

        //where is this driver from? why not move this line out of loop
        Actions actions = new Actions(driver);

        actions.MoveToElement(element).Click().Perform();
    }
}

// I simplified this code
public IWebElement getKeyElement(string searchKeys)
{
    var keys = string.Join(" and ", searchKeys.Split(",").Select(searchKey => $"contains(text(),'{searchKey}')"));

    //check keys in debug mode
    return waitForVisibility(By.XPath($"//*[{keys}]"));
}

public IWebElement waitForVisibility(By element)
{
    //I guess the issue is in Until. it is a Func<> but you may give it a result.
    return new WebDriverWait(driver, TimeSpan.FromSeconds(5))
    .Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(element));
}

祝你好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM