2015-11-09 28 views
1

我試圖通過使用此代碼在其中將按照參數文本(buttonName)單擊該按鈕的網頁上查找按鈕(預覽/編輯)。使用Xpath查找隱藏/禁用元素

頁面的行爲{當頁面加載時,它顯示你'編輯'按鈕。如果你點擊修改,然後它顯示「預覽」按鈕,反之亦然}

WebBrowserElement parentElement = waitForElement(By.xpath("//div[@class='serviceHeaderInner']")); 
String s = parentElement.toString(); 
if (s.contains(buttonName) && (parentElement.waitForElement(By.xpath(".//a[.='" + buttonName + "']"))).isDisplayed()) { 
    WebBrowserElement buttonElement = parentElement.waitForElement(By.xpath(".//a[.='" + buttonName + "']")); 
    buttonElement.click(); 
} else { 
    debugPrintln("  -> Invalid buttonName '"+ buttonName +"'"); 
} 

在測試情況下,我試圖通過,

「預覽」

「預覽」

「編輯」

「編輯」

由於加載網頁時,會出現'編輯'按鈕並在測試用例中兩次傳遞'預覽'參數。它直接轉到其他等。另外,傳遞'編輯'參數也會轉到else部分。我猜這個「如果」條件有問題。但是,爲了使更多的活力「應該檢查其中包含‘禁用’文本ACC類快照,使這一問題能夠在第一點防止

快照:。 12

回答

0

什麼值被分配到那時你投parentElement字符串?而不是做

.toString();

我認爲你需要做

.getAttribute("value");.getAttribute("text");。或者乾脆.text;

我們可以瓚如果陳述稍微有點正確,則可以這樣做。當頁面加載或等待期間,這些按鈕是否顯示?

也許我們可以試試這個,讓我們不要使用父元素,因爲在這裏它似乎不需要。這可重構多一點,但是讓我們先試試:

if (findElement(By.xpath("//a[text()='" + buttonName + "']"))).isDisplayed()) { 
    WebBrowserElement buttonElement = findElement(By.xpath("//a[text()='" + buttonName + "']")); 
    buttonElement.click(); 
} else { 
    debugPrintln("  -> Invalid buttonName '"+ buttonName +"'"); 
} 
+0

我編輯了這個問題,希望它有幫助。謝謝。 – Prago

+0

這樣做更有意義。 聲明buttonName在哪裏?在這種情況下,它似乎永遠不會包含buttonName,因爲它只是指向一個div元素,並不會在意它是什麼。 –

+0

我試圖把條件放在if塊中,是否parentElement獲取一些文本並試圖轉換成字符串,並查找它是否包含文本,因爲buttonName被傳遞。 除此之外,試圖找出是否顯示該按鈕。 buttonName在if塊內聲明。 – Prago

0

你可以使用下面的方法點擊基於名稱的按鈕。在此方法上使用try{} catch(){}來處理按鈕不存在的情況。

// button names can be one of [Edit, Preview] 
// Provided you have only one button each on the name of Edit and Preview on your page 
public void clickButtonBasedOnName(String buttonName) { 
    waitForElement(By.linkText(buttonName)).click(); 
}