2009-09-18 41 views
5

我使用Selenium RC與Chrome模式的Firefox來自動化Web應用程序的測試用例。我正在使用TestNG框架和Eclipse編寫Java腳本。現在的主要觀點:Selenium RC在XPath中遇到問題

我遇到了Selenium RC用於識別某些XPath的問題。我使用適用於Firefox的XPath-Checker擴展來驗證我的XPath,並顯示沒有錯誤。然後我在Selenium IDE中試用它來確保XPath被識別。即使IDE識別該元素。但它的硒鋼筋混凝土只是不承認它。有什麼我可以做的糾正這一點?

具體來說,我想點擊給出一個特定區域:

html/body/form/div[@id='someid1']/div[@class='someClass']/div[@id='someid2']/div[@id='someid3']/div[@id='someid4']/div[@title='titleOfTheElement'] 

然後我也試過:

//div[@title='titleOfTheElement'] 
xpath=//div[@title='Automated User'] 
xpath=/descendant::div[@title='Automated User'] 

還是什麼都沒有!

1)有人可以請建議可能是錯的,或者Selenium是否知道XPath有問題?

2)是否有任何插件(類似於XPath檢查器)幫助我們按照Selenium RC的方式來看待事物?這樣我們可以確定RC是否會識別XPath。

感謝,
無限

這裏是硒代碼:

selenium.click("somelink"); 
selenium.waitForPageToLoad("30000"); 

boolean flag=false 
    do{ 
    if (selenium.isTextPresent("Some text on the page which loads last")) 
    { 
     flag=true 
    } 
    }while(flag=false); 


selenium.click("locator for area which is driving me crazy"); 

現在,在最後一步,如果我是點擊其他地方在頁面上(指其他一些定位器)的點擊將工作。

該地區的HTML看起來像這樣:

<div id="someid1" style="overflow: hidden;"> 
<div id="someid2" title="title1" class="someclass">title1</div> 
<div id="someid3" title="title2" class="someclass">title2</div> 
<div id="someid4" title="required title" class="someclass">required title</div> 
<div id="someid5" title="title3" class="someclass">title3</div> 
<div id="someid6" title="title4" class="someclass">title4</div> 
<div id="someid7" title="title5" class="someclass">title5</div></div> 

感謝您尋找到這樣的負載。 :-)

+0

你能發佈你的Selenium RC命令嗎?您是否在發送點擊命令之前等待頁面加載? – 2009-09-18 11:56:16

回答

6

我不知道這是否是正確的有一個標題屬性的div中。是不是有你可以用它來查找元素的另一個屬性?

總之,這裏的定位器的CSS版本,在情況下,它的工作原理:

css=div[title='Automated User'] 
+0

謝謝你的答案!使用CSS是非常好的!我錯誤地嘗試CSS爲css = div [@ title ='Automated User'] - 愚蠢的我! – Mugen 2009-09-18 13:21:26

+0

很高興看到它的工作:) – Santi 2009-09-18 13:35:06

0

確定該頁面與RC正確加載,即你看到瀏覽器中打開和網頁加載?

+0

謝謝你回覆傑里米。是的,我可以看到頁面正確打開。其他領域的XPath似乎工作正常。它在一個它根本不會認識的特定領域。 單擊此特定區域中的不同元素會對頁面中的另一個div進行一些更改。這可能與問題有關嗎? – Mugen 2009-09-18 12:53:44

2

我們在Firefox上運行Selenium測試時曾經與XPath表達式的問題。

  • 您是否試過用不同的瀏覽器運行相同的測試?
  • 我記得我們替換爲星號標誌(*),並且幫助所有元素名稱。即

    // * [@ ID = 'someid1']/* [@類= 'SomeClass的']/* [@ ID = 'someid2']

2

從你的榜樣,你應該能夠使用目標元素的ID:

selenium.click("id=someid4"); 

雖然我懷疑這些ID是動態生成的,並且您無法使用此方法。

我看不出有任何理由以下XPath是行不通的:

selenium.click("//div[@title='required title']"); 

其他方式的目標將是:

selenium.click("css=div[title='required title']"); 
selenium.click("css=.someclass:nth-child(4)"); //must be 4th child of parent element 

是div您的實際目標元素?它是否響應點擊事件?如果有一個響應點擊的子元素,那麼您需要定位該元素。此外,您可以嘗試使用以下命令進行故障診斷:

mouseDown 
mouseUp 
fireEvent 
+0

謝謝我的div不是實際的目標「它沒有響應」必須讓硒按嵌套標記:-) – Blundell 2011-06-13 13:48:13

0

可能是您的腳本比網頁快,所以請嘗試等待一段時間。 像你可以使用waitForCondition("selenium.isElementPresent("ElementID")","20000");

就像xpath.try這似乎nothg worng這可能會幫助你。