简体   繁体   中英

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. 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 !?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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