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

问题描述 投票:1回答:2

我在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并出现错误:

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)'
c# selenium xpath css-selectors webdriverwait
2个回答
2
投票
从细节上没有定论,为什么会看到

StaleElementReferenceException

。但是,该元素是Angular元素,并且在<span>元素的子<a>标记内。因此,您必须为所需的ElementToBeClickable()引入WebDriverWait

0
投票
此错误通常是由于用户尝试执行某些操作而更改或删除了元素引起的。您可以尝试捕获异常,然后再次单击
© www.soinside.com 2019 - 2024. All rights reserved.