【问题标题】:How to click an icon in div? selenium c#如何单击div中的图标?硒 c#
【发布时间】:2020-02-18 13:10:41
【问题描述】:

我需要点击一个 div 图标。这是html:

<div id="commandBarIconExcel"
 tabindex="0" aria-label="Exp" role="btn" 
class="commandBarItems__container"></div>

我尝试过的:

  1. driver.FindElement(By.CssSelector("div#commandBarIconExcel")).Click();

  2. driver.FindElement(By.xpath("/html/body/div[1]/div/div/div/div[2]/main/div/div[2]")).Click();

  3. driver.FindElement(By.Id("commandBarIconExcel")).Click();

  4. new WebDriverWait(driver, TimeSpan.FromSeconds(100)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible((By.CssSelector("div#commandBarIconExcel"))));

  5. new Actions(driver).Click(driver.FindElement(By.XPath("//div[@id='commandBarIconExcel']"))).Perform();

他们都给出了无法定位的错误。请问有什么建议吗? 当我们从上面的一些下拉菜单中进行选择后,这个 div 就会打开。

【问题讨论】:

  • 本帖当前html的div中没有​​a标签,这样对吗?
  • 当您尝试单击图标时页面是否已完全加载?可能是时间问题,可能元素尚未加载
  • @Nina 可以分享您尝试访问的完整页面吗?
  • 抱歉,这是不可能的......场景:从下拉列表中选择名称和日期范围,打开一个新的 div,我想按下该 div 上的按钮
  • 在 ASPX 页面中,ID 是组成的,你在调试时检查过 ID 名称吗?

标签: c# google-chrome selenium-webdriver


【解决方案1】:

我能够使用 JavaScriptExecutor 做到这一点

IWebElement elementa = driver.FindElement(By.CssSelector("div#commandBarIconExcel"));
IJavaScriptExecutor executor1 = (IJavaScriptExecutor)driver;
executor1.ExecuteScript("arguments[0].click()", elementa);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 2022-01-17
    • 1970-01-01
    • 2021-03-17
    相关资源
    最近更新 更多