1

I'm trying to get some IWebElement in other specific IWebElement which I find, but it doesn't work.

Here I find all of the repeated HTML blocks, it works OK:

List<IWebElement> allIwebElementBlocks = InitData.Driver.FindElements(By.XPath("//div[contains(@id, 'card-trade-point')]/.")).ToList();

But when I'm trying to find IWebElement in IWebElement (which I have in List) this code give always information about the first element in List.

Target part of Html page:

<div class="LoadNamespace-TradePointsList--container-217">
    <div class="MuiPaper-root-18 MuiPaper-elevation1-21 MuiPaper-rounded-19 MuiCard-root-242 LoadNamespace-TradePointsList--item-218" id="card-trade-point-4">
        <div class="MuiCardContent-root-243">
            <h2 class="MuiTypography-root-140 MuiTypography-headline-145 MuiTypography-gutterBottom-157" id="attr-name">
                Test 223 name</h2>
            <p class="MuiTypography-root-140 MuiTypography-body1-149" id="attr-address">Address test3</p>
            <span class="MuiTypography-root-140 MuiTypography-caption-150">Address</span>
        </div>
        <div class="MuiCardActions-root-244 LoadNamespace-TradePointsList--buttonsWrapper-219">
            <button tabindex="0" class="MuiButtonBase-root-272 MuiButton-root-246 MuiButton-text-248 MuiButton-textPrimary-249 MuiButton-outlined-254 MuiButton-outlinedPrimary-255 MuiButton-sizeSmall-269 MuiCardActions-action-245" type="button" id="btn-registers"><span class="MuiButton-label-247">Box</span><span class="MuiTouchRipple-root-275"></span></button>
            <button tabindex="0" class="MuiButtonBase-root-272 MuiButton-root-246 MuiButton-text-248 MuiButton-textPrimary-249 MuiButton-outlined-254 MuiButton-outlinedPrimary-255 MuiButton-sizeSmall-269 MuiCardActions-action-245" type="button" id="btn-cashiers"><span class="MuiButton-label-247">User</span><span class="MuiTouchRipple-root-275"></span></button>
            <button tabindex="0" class="MuiButtonBase-root-272 MuiButton-root-246 MuiButton-contained-257 MuiButton-containedPrimary-258 MuiButton-raised-260 MuiButton-raisedPrimary-261 MuiButton-sizeSmall-269 LoadNamespace-TradePointsList--editButton-220 MuiCardActions-action-245" type="button" id="btn-edit"><span class="MuiButton-label-247"><svg class="MuiSvgIcon-root-69 LoadNamespace-TradePointsList--iconEdit-221" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34a.9959.9959 0 0 0-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"></path><path fill="none" d="M0 0h24v24H0z"></path></svg></span><span class="MuiTouchRipple-root-275"></span></button>
        </div>
    </div>
    <div class="MuiPaper-root-18 MuiPaper-elevation1-21 MuiPaper-rounded-19 MuiCard-root-242 LoadNamespace-TradePointsList--item-218" id="card-trade-point-9">
        <div class="MuiCardContent-root-243">
            <h2 class="MuiTypography-root-140 MuiTypography-headline-145 MuiTypography-gutterBottom-157" id="attr-name">
                One more name</h2>
            <p class="MuiTypography-root-140 MuiTypography-body1-149" id="attr-address"><i>Empty</i></p>
            <span class="MuiTypography-root-140 MuiTypography-caption-150">Address</span>
        </div>
        <div class="MuiCardActions-root-244 LoadNamespace-TradePointsList--buttonsWrapper-219">
            <button tabindex="0" class="MuiButtonBase-root-272 MuiButton-root-246 MuiButton-text-248 MuiButton-textPrimary-249 MuiButton-outlined-254 MuiButton-outlinedPrimary-255 MuiButton-sizeSmall-269 MuiCardActions-action-245" type="button" id="btn-registers"><span class="MuiButton-label-247">Box</span><span class="MuiTouchRipple-root-275"></span></button>
            <button tabindex="0" class="MuiButtonBase-root-272 MuiButton-root-246 MuiButton-text-248 MuiButton-textPrimary-249 MuiButton-outlined-254 MuiButton-outlinedPrimary-255 MuiButton-sizeSmall-269 MuiCardActions-action-245" type="button" id="btn-cashiers"><span class="MuiButton-label-247">User</span><span class="MuiTouchRipple-root-275"></span></button>
            <button tabindex="0" class="MuiButtonBase-root-272 MuiButton-root-246 MuiButton-contained-257 MuiButton-containedPrimary-258 MuiButton-raised-260 MuiButton-raisedPrimary-261 MuiButton-sizeSmall-269 LoadNamespace-TradePointsList--editButton-220 MuiCardActions-action-245" type="button" id="btn-edit"><span class="MuiButton-label-247"><svg class="MuiSvgIcon-root-69 LoadNamespace-TradePointsList--iconEdit-221" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34a.9959.9959 0 0 0-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"></path><path fill="none" d="M0 0h24v24H0z"></path></svg></span><span class="MuiTouchRipple-root-275"></span></button>
        </div>
    </div>
    <div class="MuiPaper-root-18 MuiPaper-elevation1-21 MuiPaper-rounded-19 MuiCard-root-242 LoadNamespace-TradePointsList--item-218" id="card-trade-point-2">
        <div class="MuiCardContent-root-243">
            <h2 class="MuiTypography-root-140 MuiTypography-headline-145 MuiTypography-gutterBottom-157" id="attr-name">
                Third name</h2>
            <p class="MuiTypography-root-140 MuiTypography-body1-149" id="attr-address">Address xx</p>
            <span class="MuiTypography-root-140 MuiTypography-caption-150">Address</span>
        </div>
        <div class="MuiCardActions-root-244 LoadNamespace-TradePointsList--buttonsWrapper-219">
            <button tabindex="0" class="MuiButtonBase-root-272 MuiButton-root-246 MuiButton-text-248 MuiButton-textPrimary-249 MuiButton-outlined-254 MuiButton-outlinedPrimary-255 MuiButton-sizeSmall-269 MuiCardActions-action-245" type="button" id="btn-registers"><span class="MuiButton-label-247">Box</span><span class="MuiTouchRipple-root-275"></span></button>
            <button tabindex="0" class="MuiButtonBase-root-272 MuiButton-root-246 MuiButton-text-248 MuiButton-textPrimary-249 MuiButton-outlined-254 MuiButton-outlinedPrimary-255 MuiButton-sizeSmall-269 MuiCardActions-action-245" type="button" id="btn-cashiers"><span class="MuiButton-label-247">User</span><span class="MuiTouchRipple-root-275"></span></button>
            <button tabindex="0" class="MuiButtonBase-root-272 MuiButton-root-246 MuiButton-contained-257 MuiButton-containedPrimary-258 MuiButton-raised-260 MuiButton-raisedPrimary-261 MuiButton-sizeSmall-269 LoadNamespace-TradePointsList--editButton-220 MuiCardActions-action-245" type="button" id="btn-edit"><span class="MuiButton-label-247"><svg class="MuiSvgIcon-root-69 LoadNamespace-TradePointsList--iconEdit-221" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34a.9959.9959 0 0 0-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"></path><path fill="none" d="M0 0h24v24H0z"></path></svg></span><span class="MuiTouchRipple-root-275"></span></button>
        </div>
    </div>
</div>

Full method code:

public IEnumerable<Block> GetCards()
    {
        List<IWebElement> allIwebElementBlocks = InitData.Driver.FindElements(By.XPath("//div[contains(@id, 'card-trade-point')]/.")).ToList();
        var outBlockList = new List<Block>();
        foreach (IWebElement webBlock in allIwebElementBlocks)
        {
            //Here I'm trying to get information about IWebElement in a current loop, but I receive first element in List*
            var tmpName = webBlock.FindElement(By.XPath("//h2[contains(@id, 'attr-name')]")).Text;
            var blc = new Block(webBlock);
            outBlockList.Add(blc);
        }
        return outBlockList;
    }

In the end, I want to find IWebElements from second and next elements for working with it. *For example, I need to get the text "Third name", but I receive text "Test 223 name".

2

Change var tmpName = webBlock.FindElement(By.XPath("//h2[contains(@id, 'attr-name')]")).Text; to var tmpName = webBlock.FindElement(By.XPath(".//h2[contains(@id, 'attr-name')]")).Text;.

This should work (see details here).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.