【问题标题】:How I can add click function in this code if id and xpath are not working如果 id 和 xpath 不起作用,我如何在此代码中添加点击功能
【发布时间】:2017-03-16 17:47:05
【问题描述】:

这是应用程序代码

<div class="btn-group" style="margin-top: -10px; box-shadow: none !important;">
            <a class="btn btn-clear store-name headerActive" style="margin-left: 0px !important;" ng-click="account();" _href="#/app/account-addresses">
              <div class="left-logo ng-binding">SK</div><h5 class="logo-name ng-binding">sathish kumar krish</h5>
            </a>
      </div>

它是我的 webdriver 脚本。

driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/div/div[1]/div/a")).click();

【问题讨论】:

  • 我用过,xpath,className,css 选择器。没有任何作用。我怎么能点击链接?
  • 不工作?意思是,元素没有被识别?或者元素不可见?或者该元素不可点击?抛出了什么异常?
  • 它在 IFRAME 中吗?

标签: css xpath selenium-webdriver css-selectors classname


【解决方案1】:

试试这个 -

WebDriverWait wait = new WebDriverWait(Driver, 10); // Wait for 10 seconds.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[2]/div[1]/div/div/div[1]/div/a")));
WebElement element = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/div/div[1]/div/a"));
element.click();

【讨论】:

    【解决方案2】:

    我也遇到过类似的问题,但我不确定是什么原因造成的。在这种情况下,您可能会使用 JavaScript 点击事件。如下所示,

    WebElement element = driver.findElement(By.id("<<<Your ID>>>"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", element);
    

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      相关资源
      最近更新 更多