简体   繁体   English

单击IwebElement Selenium / C#的子级

[英]Click on a child of a IwebElement Selenium/C#

I am looping through a list of Web elements, trying to click on each of the elements one by one. 我正在遍历Web元素列表,尝试逐一单击每个元素。 Even though in debug mode I can clearly see that the desired element has been passed as parameter to a method, the program is constantly clicking the first element in the list. 即使在调试模式下,我也可以清楚地看到所需的元素已作为参数传递给方法,但程序会不断单击列表中的第一个元素。

 private void CompleteForm(IWebElement element)
    {
        if (element == null) throw new ArgumentNullException(nameof(element));

        //Open the form and wait for it to load

        Wait.Until(
            ExpectedConditions.ElementToBeClickable(
                element.FindElement(By.XPath("//td/a[contains(@href, '/Shop')]")))).Click();
        Wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("SaveShop")));

        //Store the original values
        GetTheValues();

        //Submit the save button
        Wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("SaveShop"))).Click();
        Browser.Navigate().Back();
    }

I'm not sure what you are trying to do. 我不确定您要做什么。 But when you call element.FindElement(By.XPath("//td/a[contains(@href, '/Shop')]")) , I assume you want to do a relative search within the descendants of element !? 但是,当您调用element.FindElement(By.XPath("//td/a[contains(@href, '/Shop')]")) ,我假设您想在element的后代中进行相对搜索!

If so, you need to specify the . 如果是这样,则需要指定. before the // , bc otherwise you are searching the entire document. //之前,否则您将搜索整个文档。

Fixed code: 固定代码:

Wait.Until(
    ExpectedConditions.ElementToBeClickable(
        element.FindElement(By.XPath(".//td/a[contains(@href, '/Shop')]")))).Click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM