Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Selenium contains()未找到正确的元素_C#_Selenium - Fatal编程技术网

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

C#Selenium contains()未找到正确的元素,c#,selenium,C#,Selenium,由于某种原因,我一直收到返回给我的错误文件。我一直收到一个名为FC6的文件 我运行了调试器,搜索变量始终保持FC5,我猜它与contains()函数有关,但我在其他任何地方都没有遇到问题 正在寻找解决此问题的方法,请提前感谢 public override void selectFiles() { foreach (string key in searchKeys) { IWebEle

由于某种原因,我一直收到返回给我的错误文件。我一直收到一个名为FC6的文件

我运行了调试器,搜索变量始终保持FC5,我猜它与contains()函数有关,但我在其他任何地方都没有遇到问题

正在寻找解决此问题的方法,请提前感谢

        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
复制后,请将代码粘贴到我的visual studio

首先,这里是WebDriverWait的定义。直到:

public TResult Until<TResult>(Func<T, TResult> condition){}
以下是对代码的一些建议:

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));
}
public override void selectFiles()
{
//您应该在循环之前还是之后运行base.selectFiles()?base.selectFiles()没有任何逻辑?
foreach(searchKeys中的字符串键)//循环中从未使用过键,严重吗?
{
IWebElement keyElement=base.getKeyElement(“FC5”);
//getKeyElement支持多个键,为什么每次循环都会得到FC5?
//如果不需要再次获取密钥,您是否应该移到行上方
IWebElement parentElement=keyElement.FindElement(By.XPath(“./parent::*”);
IWebElement element=parentElement.FindElement(By.XPath(“//*[contains(text(),'Download')]”);//这是'Download'键吗?
//这个司机是从哪里来的?为什么不把这条线移出环路
动作动作=新动作(驱动);
actions.MoveToElement(元素)。单击()。执行();
}
}
//我简化了这个代码
公共IWebElement getKeyElement(字符串搜索键)
{
var keys=string.Join(“and”,searchKeys.Split(“,”).Select(searchKey=>$”包含(text(),“{searchKey}”)));
//在调试模式下检查键
返回waitForVisibility(By.XPath($“/*[{keys}]”);
}
公共IWebElement waitForVisibility(按元素)
{
//我想这个问题一直到。这是一个函数,但你可以给出一个结果。
返回新的WebDriverWait(驱动程序,TimeSpan.FromSeconds(5))
.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.element可见(element));
}

祝你好运。

异常处理是我最讨厌的事情,而你最讨厌的是。您捕获并吞下了所有异常,这是异常处理的一大罪过。我经常链接两篇关于这个主题的文章:|可以理解,但是你能帮我解决这个问题吗?如果你有这样的代码,很可能你吞下了异常,给了你需要的信息。您的整个代码也令人困惑:
搜索键
被明确指示为一个集合。然而
getKeyElement(字符串搜索键)
get是一个字符串吗<代码>选择文件()在该代码的任何地方都没有使用。要么它不重要,要么你没有告诉我们你是如何使用它的。实际上,只需完全删除try/catch。抓住例外是一个愚蠢的想法,它会咬你全身。永远不要那样做。
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));
}