首页 > 解决方案 > 可聚焦值为 false 的 xPath 元素

问题描述

在此处输入图像描述Dom:我需要单击给定 DOM 中的“svg”或“path”元素。我正在使用所有东西来解决这个问题,但总而言之,不可能找到它。我想这可能是一个问题:“可聚焦”值设置为false。

我需要创建方法,其目标是:1)将元素与我想删除的许多元素区分开来:2)单击并删除一个元素

我已经尝试过的是首先区分一个元素(filterChip) - 它可以正常工作。下一步是获取一个负责单击的元素(UI 上的“X”按钮)。直接在 DOM 上的两个元素可能是获取它的选项: - svg
- path


filterChipLocator -"//div[contains(@class,'chip')]//child::span[contains(text(),'{0}')]";

 public void DeleteFilterChip(string filterInput)
        {
           var filterChip = driver.FindElement(By.XPath(string.Format(filterChipLocator, filterInput)));
 var deleteFilterChip = filterChip.FindElement(By.XPath("//parent::div[contains(@class,'chip')]//svg[@role = 'presentation']"));
        }

DOM

<div class="jss480 jss481 jss520 jss546">
    <div class="jss481" style="line-height: 64px;">
        <div role="button" class="jss656 jss662 chip" tabindex="0" style=""><span class="jss675" style="">Test1</span>
            <svg class="jss358 jss676" focusable="false" viewBox="0 0 24 24" aria-hidden="true" role="presentation">
                <path d="M12 2C6.47 2 2 6.47 2 12s4.47 10 10 10 10-4.47 10-10S17.53 2 12 2zm5 13.59L15.59 17 12 13.41 8.41 17 7 15.59 10.59 12 7 8.41 8.41 7 12 10.59 15.59 7 17 8.41 13.41 12 17 15.59z"></path>
            </svg>
        </div>
    </div>
    <div class="jss481" style="line-height: 64px;">
        <div role="button" class="jss656 jss662 chip" tabindex="0"><span class="jss675">Test2</span>
            <svg class="jss358 jss676" focusable="false" viewBox="0 0 24 24" aria-hidden="true" role="presentation">
                <path d="M12 2C6.47 2 2 6.47 2 12s4.47 10 10 10 10-4.47 10-10S17.53 2 12 2zm5 13.59L15.59 17 12 13.41 8.41 17 7 15.59 10.59 12 7 8.41 8.41 7 12 10.59 15.59 7 17 8.41 13.41 12 17 15.59z"></path>
            </svg>
        </div>
    </div>
</div>

由于 html 不正确的缩进,我也在图片中上传:

标签: c#seleniumselenium-webdriverselenium-chromedriver

解决方案


To access the svg element you need to use

//*[name()='svg']

OR

//*[local-name()='svg']

Try the following xpath to access the svg elements.

//div[contains(@class,'chip')]//*[name()='svg'][@role = 'presentation']

OR

//div[contains(@class,'chip')]//*[local-name()='svg'][@role = 'presentation']

OR

 //div[contains(@class,'chip')]//*[name()='svg'][@role = 'presentation']/*[name()='path']

Check this link :- How to identify svg element.


推荐阅读