2013-07-11 84 views
8

我創造了另一個問題,我認爲這是此錯誤的原因:Why does the Selenium Firefox Driver consider my modal not displayed when the parent has overflow:hidden?「元素當前不可見,因此可能不會與」互動「,但另一個是?

硒版本2.33.0
火狐司機

導致該錯誤代碼:

 System.Threading.Thread.Sleep(5000); 
     var dimentions = driver.Manage().Window.Size; 
     var field = driver.FindElement(By.Id("addEmployees-password")); //displayed is true 
     field.Click(); //works fine 
     var element = driver.FindElement(By.Id(buttonName)); //displayed is false 
     element.Click(); //errors out 

它試圖點擊的按鈕:

<div id="addEmployees" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="addEmployeesLabel" aria-hidden="true"> 

    <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
     <h3>Add Employee</h3> 
    </div> 

    <div class="modal-body"> 
     <p class="alert alert-info"> 
      <input name="addEmployees-username" id="addEmployees-username" /> 
      <input name="addEmployees-password" id="addEmployees-password" type="password" /> 
      <input name="addEmployees-employee" id="addEmployees-employee" /> 
     </p> 
    </div> 

    <div class="modal-footer"> 
     <button name="addEmployees-add" id="addEmployees-add" type="button" class="btn" data-ng-click="submit()">Add</button> 
    </div> 

</div> 
  • 如果我將呼叫更改爲FindElements,那麼我得到一個元素,因此頁面上沒有其他內容。
  • 如果我FindElement上的按鈕前右側出現一個字段,說addEmployees-employee,然後addEmployees-employeedisplayed
  • 在瀏覽器本身,它顯示了罰款,我需要做的是實際點擊按鈕和期望的行爲執行,但webdriver拒絕考慮顯示的元素

它是如何可以考慮顯示一個字段而不是另一個字段?

enter image description here

在右下角的添加按鈕模態,所有其他元素被顯示=真

窗口尺寸每driver.Manage().Window.Size;
1200x645的元件的位置是:每driver.FindElement(By.Id(buttonName)).Location
800x355y 元素尺寸爲:51x30每driver.FindElement(By.Id(buttonName)).Size
密碼元素的位置是:552x233y每driver.FindElement(By.Id("addEmployees-password")).Size

+1

我們用類似於這樣:'最好推遲(Waits.elementDisplayed(網絡元素放在這裏));'在這裏工作。你的自動化框架中是否有這樣的機制? – Brian

+0

@布賴恩的C#驅動程序隱式等待,我已經配置了10秒,事情是,在它被標記爲顯示之前發生的元素,但按鈕不是,踢球者是另一種模式是相同的但編輯而不是添加工作正常! –

+0

當你看它時,這兩個元素在'DOM'中總是可見嗎? – Brian

回答

4

Selenium WebDriver不僅檢查不透明度!= 0,visibility = true,height> 0,並且在當前元素上顯示!= none,但它還搜索DOM的祖先鏈以確保沒有父元素也匹配這些跳棋。 (UPDATE看JSON線代碼,所有綁定重提後,社署也需要overflow != hiddenaswell asa fewothercases。)

我會重組代碼@布賴恩建議之前做兩件事情。

  1. 確保「div.modal_footer」元素沒有任何理由讓SWD認爲它不可見。

  2. 注入一些JavaScript以突出顯示瀏覽器中存在問題的元素,這樣你就知道你已經選擇了正確的元素。您可以使用this gist作爲起點。如果按鈕以黃色邊框突出顯示,那麼您知道您選擇了正確的元素。如果不是,則表示所選元素位於DOM中的其他位置。如果是這種情況,您可能沒有像您期望的那樣擁有唯一的ID,這會使DOM的操作非常混亂。

如果我不得不猜測,我會說第二個是你遇到的。這也發生在我身上,開發人員重複使用元素ID,導致您應該找到哪個元素的爭用。

+0

我認爲它的頭號。我在OP中添加了我的另一個問題來說明原因。 –

+0

啊!我懂了。那麼,在看了你的其他帖子之後,我發現在SWD代碼中Overflow:隱藏的項目也被認爲是不顯示的https://github.com/SeleniumHQ/selenium/blob/d66d19890f10025ec81ae4ad68e247f64835ed1d/javascript/atoms/dom.js#L631 – bbbco

+0

它必須特定的Firefox,因爲鉻驅動程序沒有這個問題。 –

1

在與您討論這個問題後,我認爲最好的解決方案(至少現在是)將按鈕移出頁腳,並放入其中。

這是你想要的(現在):

<div class="modal-body"> 
    <p class="alert alert-info"> 
     <input name="addEmployees-username" id="addEmployees-username" /> 
     <input name="addEmployees-password" id="addEmployees-password" type="password" /> 
     <input name="addEmployees-employee" id="addEmployees-employee" /> 
     <button name="addEmployees-add" id="addEmployees-add" type="button" class="btn" data-ng-click="submit()">Add</button> 
    </p> 
</div> 

而不是這個:

<div class="modal-body"> 
    <p class="alert alert-info"> 
     <input name="addEmployees-username" id="addEmployees-username" /> 
     <input name="addEmployees-password" id="addEmployees-password" type="password" /> 
     <input name="addEmployees-employee" id="addEmployees-employee" /> 
    </p> 
</div> 

<div class="modal-footer"> 
    <button name="addEmployees-add" id="addEmployees-add" type="button" class="btn" data-ng-click="submit()">Add</button> 
</div> 
5

Brian的迴應是正確的:使用顯式的等待與Thread.sleep()方法。睡眠()通常是脆弱的,你不必要地失去5秒鐘,而且這對於自動化測試來說只是一個非常爛的做法。 (花了我很長的時間才知道,所以你並不孤單。)

避免隱含的等待。它們通常用於添加到DOM中的新項目,而不是用於像模態那樣的事情的轉換變爲活動。

顯式等待有一套ExpectedConditions(detailed in the Javadox),它可以幫助您解決這些問題。使用與您的下一個操作所需狀態相匹配的ExpectedCondition。

另外,也參見Ian Rose's great blogpost on the topic

+0

我的代碼是用C#編寫的,文檔在java(-1)中。研究顯式等待(+1),並發現這一點:http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp所以我既不投票也不投票。 –

1

我有相同的問題元素不可見,所以不能與之交互。它剛剛解決了。我更新了我的硒獨立服務器。以前的版本是2.33.0現在是2.35.0

+0

對不起,我想知道如何解決這個。我使用最新的量角器,chromedriver 2.32,硒版本是3.5.3。當我想測試模態頁腳中的按鈕時,它仍然'元素不可見'。當我將按鈕從頁腳移到模態體時,一切都很好。 – simmone

+0

你可以檢查這是否適用於任何瀏覽器?你能檢查元素是否被正確識別? –

+0

我找到了!原因很有趣,因爲量角器啓動瀏覽器只有正常的一半寬度,所以當然不可見!我添加「browser.driver.manage()。window()。maximize();」在測試開始時,量角器啓動全屏模式瀏覽器進行測試,解決! – simmone

0

在我的情況的元素已經存在於網頁,但它被禁用, 所以這個沒有工作(蟒蛇):

wait.until(lambda driver: driver.find_element_by_id("myBtn")) 
driver.find_element_by_id("myBtn").click() 

它失敗,錯誤:

「Element is not currently visible and so may not be interacted with" 

解決我的問題,我不得不等待幾秒鐘(time.sleep(5)),直到元素變得可見。

您也可以使用JavaScript,Python的例子使元素:

driver.execute_script("document.getElementById('myBtn').disabled='' ") 
driver.execute_script("document.getElementById('myBtn').click() ") 
相關問題