如何使用Selenium和C#识别具有href属性的元素 [英] How to identify the element with the href attribute using Selenium and C#

查看:184
本文介绍了如何使用Selenium和C#识别具有href属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在选择href上遇到了问题

I have a problem with select on href

<a id="1 - GovSearch" name="1 - GovSearch" title="Population and Immigration Authority" 
href="https://www.gov.il/en/Departments/population_and_immigration_authority" class="content-title 
first-result" ng-class="{ 'first-result': $first }">
 <!-- ngIf: item.Extension -->
 <span ng-class="item.SubTitle ? 'pipe-after':''" class="ng- 
     binding"> Population and Immigration Authority </span>
                                        <!-- ngIf: item.SubTitle -->
                                    </a>

我尝试使用 linktext 制作并出现错误:

I tried to make with linktext and getting error:

element.FindElement(By.LinkText("Population and Immigration Authority")).Click();

出现此错误:

OpenQA.Selenium.StaleElementReferenceException: 'stale element reference: element is not attached to the page document (Session info: chrome=83.0.4103.61)'


推荐答案

对于为什么看到 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();
    


  • 这篇关于如何使用Selenium和C#识别具有href属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆