2013-08-07 15 views
0

在此處發佈此問題之前,我已搜索論壇。我找到了一些答案,但我無法從答案中獲得成功。我的問題是如何使用java在Webdriver中檢查鏈接是否已啓用?

如何使用java在Webdriver中檢查鏈接是否已啓用。請查找相同的屏幕截圖。

enter image description here

enter image description here

我寫的代碼如下:

WebElement textlink = driver.findElement(By.id(OR.getProperty("lnkView_ID"))); 
if (textlink.isEnabled()) 
    System.out.println("View link: Enabled"); 
else 
    System.out.println("View link: Disabled"); 

請幫我出這個問題。幫助將不勝感激。

+0

報告的錯誤是什麼? –

+0

另外,您用於查找鏈接的實際定位器是什麼?你能提一下嗎? –

+1

我建議仔細檢查您使用的ID。這絕對是獨一無二的嗎?它不會每次加載頁面時都改變,或者刷新動作列表嗎?另外,你有沒有在你的腳本中預置它?除此之外,你的代碼看起來像試圖做正確的事情。 –

回答

1

嘗試以下操作:

String isDisabled = textlink.getAttribute("disabled"); 
if (isDisabled==null || !isDisabled.equals("disabled")){ 
    System.out.println("View link: Enabled"); 
}else{ 
    System.out.println("View link: Disabled"); 
} 

它看起來像屬性「禁用」是撥動它是否啓用與否,這樣你就可以檢查它使用getAttribute();

+0

抱歉,延遲迴答。它爲我工作。謝謝 –

0

檢查屬性的在標籤中禁用。由於'查看'鏈接被禁用,你可以從你的HTML中找到它的代表disabled =「disabled」。在XPath中編寫標識符或使用Id標識符,並在下面使用屬性值for disabled查找鏈接狀態。

string viewLinkXpath = "//tobody/tr/td/a"; 
IWebElement viewLinkElement = driver.FindElement(By.XPath(viewLinkXpath)); 
if(viewLinkElement.GetAttribute("disabled")!="disabled") 
{ 
    Console.WriteLine("View link enabled"); 
} 
0

請爲getAttribute提供屬性名稱作爲輸入,而不是提供屬性值。例如:

WebElement test=d.findElement(By.xpath(ORNsk.prevXpath)); 
String isDisabled=test.getAttribute("class"); 

這裏class是屬性名稱。

相關問題