【问题标题】:Not able to click Button(element) on Selenium webdriver无法单击 Selenium webdriver 上的按钮(元素)
【发布时间】:2016-08-29 10:48:52
【问题描述】:

无法单击 Selenium webdriver 上的按钮(元素)。它没有显示出这样的元素异常。

HTML:

<button id="datepicker-354-7412-title" class="btn btn-default btn-sm uib-title" tabindex="-1" ng-disabled="datepickerMode === maxMode" ng-click="toggleMode()" type="button" aria-atomic="true" aria-live="assertive" role="heading">
    <strong class="ng-binding">August 2016</strong>
</button>

Java:

driver.findElement(By.xpath("//*[@id='flip-card']/div[2]/div/div[2]/div[2]/div[1]/div/div[2]/i")).click();
driver.manage().timeouts().implicitlyWait(05, TimeUnit.SECONDS);
//driver.findElement(By.xpath("//ul[@class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/thead/tr/th/button[@id='datepicker-758-2620-title']/strong")).click();
//driver.findElement(By.xpath(".//*[@id='datepicker-961-3767-title']")).click();
//WebElement mm=driver.findElement(By.id("datepicker-1164-5186-title"));
//mm.click();
/*WebElement element=driver.findElement(By.xpath("//*[@id='datepicker-354-7412-title']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click()", element); */
driver.findElement(By.xpath("//button[@id='datepicker-354-7412-title']/strong")).click();

【问题讨论】:

  • 显示您的点击按钮代码。
  • 请将 html 和您的 Java 代码添加到问题中。
  • @Guy
  • 你能显示你的点击按钮代码吗?

标签: java selenium-webdriver


【解决方案1】:

id可能是动态的,尝试通过包含datepicker和title的部分id来定位按钮

driver.findElement(By.cssSelector("[id*='datepicker'][id*='title']")).click();

您还可以使用显式等待来确保按钮在单击之前存在/可见

WebDriverWait wait = new WebDriverWait(driver, 10);

// visible
WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[id*='datepicker'][id*='title']")));
button.click();

// or exist
WebElement button = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("[id*='datepicker'][id*='title']")));
button.click();

【讨论】:

【解决方案2】:

这应该可行:

driver.findElement(By.id("datepicker-354-7412-title")).click();

【讨论】:

    【解决方案3】:

    您是否尝试过将您的 xpath 放入 firepath 并检查它是否指向您想要的?

    您是否尝试过使用

    //strong[@class='ng-binding'] 作为你的 xpath?

    知道这是否有帮助?

    【讨论】:

    • 它使用此代码 driver.findElement(By.cssSelector("[id*='datepicker'][id*='title']")).click();
    【解决方案4】:

    使用 firefox 插件 firebug 查找 xpath: - 检查元素 - 右键单击​​元素并复制xpath

    Firebug 的 xpath 生成器非常好,生成的路径一般适用于其他浏览器。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 2014-01-04
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 2017-01-17
      相关资源
      最近更新 更多