简体   繁体   中英

Click() not working on a button but getText() works fine with same xpath in selenium webdriver

I am trying to click a button on a pop up using xpath, I tried the following code:

MySuite.driver.findElement(By.xpath("html/body/div[9]/div[3]/div/button[4]")).click();

It does not click on the intended button but when I use same xpath and use getText() method it returns me correct value:

String test=MySuite.driver.findElement(By.xpath("html/body/div[9]/div[3]/div/button[4]")).getText();
System.out.println(test);

I have tried using Implicit wait also but not to no use. Please help what all reasons can be possible for button not getting clicked.

The reason could be:

  1. Button is not click-able at that moment where you are trying to click on it. So use an explicit expected condition to be ensure that button is click-able.

    WebDriverWait wait = new WebDriverWait(driver, 10);wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));

  2. Or, Try to click on parent of button element.If it is possible.

  3. Or, Inject java script explicitly to click on required button.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM