2017-03-28 32 views
0

我正在使用的應用程序有幾個下拉按鈕具有相同的xml代碼。下面是所有下拉按鈕都相似的代碼。使用硒webdriver代碼點擊下拉按鈕時面臨的問題

<button class="btn dropdown-toggle bs-placeholder btn-default" role="button" data-toggle="dropdown" type="button" data-id="invContactList" title="" data-original-title="Nothing selected">

我有下面的代碼使用點擊按鈕

driver.findElement(By.xpath("//button[@data-toggle='dropdown']")).click(); 

使用該代碼的應用程序運行成功一次,但有它給誤差後:org.openqa.selenium .ElementNotVisibleException:元素當前不可見,因此可能不會與Command進行交互。我已經刪除瀏覽器cookies,但它不會幫助。

有人可以幫助我的根本原因和解決方案?

+0

請發佈幾個按鈕的HTML。你有沒有嘗試過一個CSS選擇器,比如「button [data-id ='invContactList']」?你確定你正在找到你想要的元素嗎?有可能你的定位器找到多個,而第一個可用的是隱藏的。 – JeffC

+0

你試過我的回答嗎? –

+0

是的它的工作..謝謝 –

回答

0

您可能需要等待元素:

WebDriverWait wait = new WebDriverWait(driver, 10); 
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@data-toggle='dropdown']"))).click(); 

OR

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@data-id='invContactList']"))).click(); 

OR

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@data-toggle='dropdown']"))).click(); 

OR

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@data-id='invContactList']"))).click(); 

在您的最終嘗試。

+0

它的工作..謝謝你 –