[英]Click() not working on a button but getText() works fine with same xpath in selenium webdriver
我正在嘗試使用xpath在彈出窗口上單擊一個按鈕,我嘗試了以下代碼:
MySuite.driver.findElement(By.xpath("html/body/div[9]/div[3]/div/button[4]")).click();
它不會單擊預期的按鈕,但是當我使用相同的xpath並使用getText()方法時,它將為我返回正確的值:
String test=MySuite.driver.findElement(By.xpath("html/body/div[9]/div[3]/div/button[4]")).getText();
System.out.println(test);
我也嘗試過使用隱式等待,但不是沒有用。 請幫助所有可能導致按鈕未被點擊的原因。
原因可能是:
在您嘗試單擊按鈕時,該按鈕不可單擊。 因此,請使用明確的預期條件來確保按鈕是可單擊的。
WebDriverWait wait = new WebDriverWait(driver, 10);wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
或者,嘗試單擊按鈕元素的父元素。
或者,顯式注入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.