【问题标题】:Selenium Issues finding element in an accordian硒问题在手风琴中寻找元素
【发布时间】:2023-03-14 17:55:01
【问题描述】:

我正在尝试单击手风琴元素内的报告。

我尝试了各种方法来识别元素,但我尝试过的都没有用。

使用 XPath 和 Id 我收到以下错误消息:

OpenQA.Selenium.ElementNotInteractableException: '元素不可交互'

这是我尝试过的代码。

static IWebDriver driver = new ChromeDriver();
static IWebElement element;

driver.FindElement(By.XPath("//*[@id=\"ContentPlaceHolder1_ReportCreator1_DataListReports_3_LinkButtonReport_0\"]")).Click();
driver.FindElement(By.Id("ContentPlaceHolder1_ReportCreator1_DataListReports_3_LinkButtonReport_0")).Click();

这是我要定位的手风琴内的 HTML,

<a id="ContentPlaceHolder1_ReportCreator1_DataListReports_3_LinkButtonReport_0" class="ReportImg" href="javascript:__doPostBack           ('ctl00$ContentPlaceHolder1$ReportCreator1$AccordionReportList_Pane_3_content$DataListReports$ctl00$LinkButtonReport','')">All Assessments</a>

【问题讨论】:

  • 再次检查您的定位器是否唯一...打开开发控制台并尝试$$("#ContentPlaceHolder1_ReportCreator1_DataListReports_3_LinkButtonReport_0")。它只返回一个元素吗?这似乎是一个疯狂的长定位器,不是独一无二的......但我以前见过这样的问题,页面上有 2 个或更多元素,第一个不可见,等等。

标签: c# selenium selenium-webdriver webdriverwait


【解决方案1】:

交互时元素可能不可见尝试使用WebDriverWait() 并等待ElementToBeClickable() 和跟随定位器。

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("ContentPlaceHolder1_ReportCreator1_DataListReports_3_LinkButtonReport_0"))).Click();

或者

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[text()='All Assessments']"))).Click();

【讨论】:

  • 感谢 Kunduk,两者都有效。不过我有一个小问题。 “ExpectedConditions”下面有一条波浪线,消息说“已弃用”。我可以将它标记为已过时,但我希望您能找到解决方案?
  • @Lars7 :使用 NuGet,搜索 DotNetSeleniumExtras.WaitHelpers,然后将该命名空间导入您的类。检查这个答案stackoverflow.com/questions/49866334/…
  • @Kundak 再次感谢,您的帮助很大
  • @Lars7 :很高兴能够提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 2021-04-17
  • 1970-01-01
  • 2011-12-23
相关资源
最近更新 更多