2012-10-23 43 views
3

我以前使用過selenium RC,但我是webdriver的新手。 我在我的應用程序中有三個鏈接。通知,消息和連接。 單擊通知時,會顯示通知下拉框。在單擊消息時,將顯示消息下拉框,並在單擊連接時顯示連接下拉框。 在腳本中,我單擊通知鏈接,等待通知下拉框,然後斷言通知收件箱。消息和連接依次相同。 通知序列正常工作。在消息中,它會單擊消息鏈接,然後掛起等待消息放置框鏈接。我知道這一點,因爲我已經在每一行之後放置了打印命令。 赫雷什是我的代碼:在IE中運行的Selenium webdriver腳本掛在Firefox中

driver.findElement(By.xpath("//a[@id='notifications-page-button']")).sendKeys("\n"); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@id='notifications-dropdown-list']//li//div[@class='message']"))); 
Assert.assertTrue(isElementPresent(By.xpath("//div[@id='notifications-dropdown-list']//li//div[@class='message']")), "Notifications drop box was not displayed"); 

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@id='messages-page-button']"))); 

driver.findElement(By.xpath("//a[@id='messages-page-button']")).sendKeys("\n"); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='sf_messages_list dropdown']//li//div[@class='message']"))); //This is the line where the script hangs. If I remove this line and the next line and continue with just the click commands, they work. But when I have this line and the next, the remaining click commands are not executed 

Assert.assertTrue(isElementPresent(By.xpath("//div[@class='sf_messages_list dropdown']//li//div[@class='message']")), "Messages drop box was not displayed"); 

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("connections-page-button"))); 
driver.findElement(By.id("connections-page-button")).click() 

赫雷什的消息部分中的HTML:

<li class="icon mm open"> 
<a id="messages-page-button" class="mm" href="#"> 
<span class="icon"></span> 
<span class="badge hidden_elem"> 
<strong> 
<em>0</em> 
</strong> 
</span> 
</a> 
<div class="dropdown-holder"> 
<div class="sf_messages_list dropdown" data-eventprefix="mainmenu_messages"> 
<div class="tb-dropdown-header">Messages</div> 
<div class="tb-dropdown-body" data-url="/messages/dropdown"> 
<div class="document-title" style="display: none; !important"> - Dropdown Messages</div> 
<div id="messages_list_view_76345" class="message-threads-listview"> 
<ul> 
<div class="no_items hidden_elem"> 
</div> 
</div> 
<div class="tb-dropdown-footer" style="display: block;"> 
</div> 
<span class="shadow-hide"></span> 
</div> 
</li> 

下面有用於通知部分的HTML:

<li class="icon mm"> 
<a id="notifications-page-button" class="mm" href="#"> 
<span class="icon"></span> 
<span class="badge hidden_elem"> 
<strong> 
<em>0</em> 
</strong> 
</span> 
</a> 
<div class="dropdown-holder"> 
<div id="notifications-dropdown-list" class="sf_notifications_list dropdown" data-eventprefix="mainmenu_notifications"> 
<div class="tb-dropdown-header">Notifications</div> 
<div class="tb-dropdown-body" data-url="/notifications/dropdown"></div> 
<div class="tb-dropdown-footer"> 
<a class="view_all" href="/notifications/view">View All Notifications</a> 
</div> 
</div> 
<span class="shadow-hide"></span> 
</div> 
</li> 

上面的代碼在IE。所以看起來問題不在於它無法找到元素。 我使用Selenium 2.25.0。我嘗試過各種版本的FF,包括3.6,7,11,13,15和16,但他們都沒有工作。 此外,腳本只是掛起。它甚至不會在eclipse中拋出錯誤。我曾經讓腳本運行了大約11個小時,仍然沒有錯誤。

請讓我知道,以防您需要更多信息來幫助我解決此問題。

謝謝!

+0

經歷正是你的一樣的情況。無法弄清楚該怎麼辦..你解決了這個問題嗎?還是有工作?請幫幫我....! :-( – Mike

回答

0

這似乎與Facebook登錄有一些聯繫。你是否正確切換窗口句柄?此外,您默認的默認時間是30秒,所以我不確定您的腳本是否可以運行11個小時而不會出錯。

您可以嘗試以下

1)我猜測className("message")不存在,你的腳本實際上是得到後停留在這一步,而不是一步。其中涉及點擊。

driver.findElement(By.id("notifications-page-button")).click(); 
wait.until(driver.findElement(By.id("messages-page-button")));#<<-- Changed the element to wait for 

//The code works untill here. But on the next command that clicks messages link, it hangs 
driver.findElement(By.id("messages-page-button")).click(); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(("username")))); 

2)刪除等元素

driver.findElement(By.id("notifications-page-button")).click(); 
#removed this wait for element 

//The code works untill here. But on the next command that clicks messages link, it hangs 
driver.findElement(By.id("messages-page-button")).click(); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(("username")))); 

UPDATE


請試試這個...

driver.findElement(By.id("notifications-page-button")).click(); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(("message")))); 

//The code works untill here. But on the next command that clicks messages link, it hangs 
driver.findElement(By.id("messages-page-button")).click(); 
wait.until(driver.findElement(By.id("connections-page-button"))); # changed it to wait for the element that you will next work with 

driver.findElement(By.id("connections-page-button")).click(); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(("connections-listview")))); 

另外,如果您provid e Web元素的HTML代碼片段,您不能確定它是否位於定位器中,我們可以幫助解決這個問題。

+0

是的,切換正常,甚至在登錄後在主窗口上執行第一個點擊命令,但之後什麼都不做,如果隱含的等待時間是30秒,那麼應該是這樣。我已經指定的參數是30秒 – Pooja

+0

做了一些編輯,更新了我的回答 – Amey

+0

感謝您的回覆。不幸的是,我試了兩個建議,但它沒有工作:( – Pooja

1

以前版本的Selenium Webdriver發生了類似的事情。我也無能爲力,發生在我身上的事情。最後,升級到最新版本的Selenium幫助我變好,但是因爲2.25。0是最新的,我將至少爲您呈現我使用的解決方法,直到更新解決它。

每當我需要點擊一個按鈕,什麼都沒有發生(至於你)。所以解決方法是,點擊按鈕時,我也發送Enter關鍵事件。

更具體:

WebElement buttonWhereClickingDoesNotWork = driver.findElement(By.id("messages-page-button"); 
buttonWhereClickingDoesNotWork.click(); 
buttonWhereClickingDoesNotWork.sendKeys(Keys.ENTER); 

是的,它的解決方法。是的,它不好。是的,它幫助了我。

而且也:不,我不知道這樣做的根本原因,因爲硒的更新我的情況幫我...

+0

感謝您的迴應。至少有一個人面對這個問題,並能理解我的情況:)。我嘗試了你建議的解決方法,但沒有幫助。我添加了命令以在單擊命令之後按Enter鍵。但是我使用Selenium 2.25.0和FF 15.你認爲我應該使用Selenium 2.22和FF 13或之前的解決方案來工作嗎? – Pooja

+0

我試圖降級到硒2.22和FF 13.試圖解決你的建議,但它沒有工作:( – Pooja

+0

太糟糕了...這是我知道的唯一的解決方法,它對我工作 –

相關問題