[英]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: 我正在尝试使用xpath在弹出窗口上单击一个按钮,我尝试了以下代码:
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: 它不会单击预期的按钮,但是当我使用相同的xpath并使用getText()方法时,它将为我返回正确的值:
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: 原因可能是:
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")));
Or, Try to click on parent of button element.If it is possible. 或者,尝试单击按钮元素的父元素。
Or, Inject java script explicitly to click on required button. 或者,显式注入Java脚本以单击所需的按钮。
WebElement element = driver.findElement(By.id("gbqfd")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", element);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.