查找id增加1 C#的元素硒



所以我试图选择一个Id为"的元素;编辑-";。问题是元素是动态的。所以可能什么都没有,5个不同的,或者一个。在每种情况下,短划线后面的数字都会根据我要选择的编辑按钮而变化。我不知道如何";选择";我想要的方法。

目前我的代码是

private ReadOnlyCollection<IWebElement> referenceEditButtons => driver.FindElements(By.Id("edit-0"));
public void EditReference(int reference)
{
this.referenceEditButtons[reference].Click();
}

问题是,从技术上讲,网页上只有一个edit-0实例。我必须为每个编辑按钮制作一个变量,我想看看我是否可以远离它,因为它会添加一堆变量和方法。那么有没有一种方法可以简化这一点;referenceEditButtons";变量能够选择任何版本的";编辑-";我需要它还是其他什么?

要选择id属性值以edit-开头的每个可用编辑按钮,可以使用以下定位器策略之一:

  • CsSelector:

    private ReadOnlyCollection<IWebElement> referenceEditButtons => driver.FindElements(By.XPath("[id^='edit-']"));
    
  • XPath

    private ReadOnlyCollection<IWebElement> referenceEditButtons => driver.FindElements(By.XPath("//*[starts-with(@id, 'edit-')]"));
    

最新更新