找到硒按钮,但未执行单击

时间:2020-01-22 09:38:46

标签: java selenium selenium-webdriver selenium-chromedriver

我想单击xpath标识的按钮,但是它不起作用。

不过,点击操作没有错误。找到该按钮,但未执行单击操作。

我尝试了以下解决方案:

解决方案1 ​​

WebElement elem = driver.findElement(By.xpath("//button[@id='btn_M9pg_']"));
Actions actions = new Actions(driver);
actions.moveToElement(elem);
actions.click(elem);
Action a = actions.build();
a.perform();

解决方案2

driver.findElement(By.xpath("//button[@id='btn_M9pg_']")).click();

能帮我吗?非常感谢!

更新1

请在日志下方找到。找不到ID为btn_D_Ir_的元素按钮,因为未执行上一次单击(//button[@id='btn_M9pg_'])。

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.id: btn_D_Ir_ (tried for 30 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at TripleA.TFT_508.main(TFT_508.java:219)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#btn_D_Ir_"}
  (Session info: chrome=78.0.3904.87)

1 个答案:

答案 0 :(得分:0)

您可以尝试使用 WebDriverWait ,如下所示:

new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='btn_M9pg_']"))).click();

您可以通过js单击,如下所示:

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