且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Selenium和C#识别具有href属性的元素

更新时间:2023-09-03 10:49:22

对于为什么看到 StaleElementReferenceException 的细节有些不确定。但是,该元素是 Angular 元素,并且在子< span> $ < a> 元素的c $ c>标记。因此,您必须为所需的 ElementToBeClickClick()诱导 WebDriverWait ,并且可以使用以下任一定位器策略作为解决方案:

A bit unconclusive from the details why you would see a StaleElementReferenceException. However, the element is an Angular element and within the child <span> tag of the <a> element. So you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies as solutions:


  • CssSelector

new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("a[id$='GovSearch'][name$='GovSearch'][title='Population and Immigration Authority'] span.ng-binding"))).Click();


  • XPath

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@title='Population and Immigration Authority']//span[@class='ng-binding' and contains(., 'Population and Immigration Authority')]"))).Click();