【问题标题】:Click an Onclick Event in selenium在 selenium 中单击一个 Onclick 事件
【发布时间】:2020-02-27 17:07:24
【问题描述】:

我正在尝试单击具有以下 html 代码的菜单图标

<a href="#" class="ctm-icon-link" onclick="show_menu('doc_107094', 1); return false;"><i class="icon-left-space icon-chevron-sign-down">&nbsp;</i></a>

doc_number - 编号每次都会改变 我的代码点击图标菜单

FindElement(By.ClassName("ctm-icon-link"));
FindElement(By.XPath("//a[@href='#']@onclick"));
FindElement(By.XPath("//a[@href="#"]@class"));

【问题讨论】:

    标签: c# selenium selenium-webdriver webdriver


    【解决方案1】:

    要相对于onclick 定位元素事件,您需要为elementToBeClickable() 诱导WebDriverWait,您可以使用以下任一Locator Strategies

    • XPath 1:

      IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='ctm-icon-link' and starts-with(@onclick, 'show_menu')]")));
      
    • XPath 2:

      IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='ctm-icon-link' and starts-with(@onclick, 'show_menu')]/i")));
      

    【讨论】:

    • 你的解决方案中所说的WebDriverWait在哪里?
    • @Jonah 那是我的错,谢谢指出,更新了答案
    猜你喜欢
    • 2013-08-03
    • 1970-01-01
    • 2021-12-21
    • 2021-08-29
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多