2016-02-23 134 views
0
<div class="ui-dialog-buttonset"> 
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover" type="button" role="button" aria-disabled="false"> 
<span> 
<br/> 
    For use to protect against or prevent actual or potential fraud, unauthorized transactions,claims or other liability. 
</span> 
<br/> 
<br/> 
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false"> 
</div> 
<span align="center">  Selection of this option will automatically log you out of the system.</span> 
</div> 
</div> 

我有兩個按鈕相同的按鈕類名稱。如何找到第一個按鈕並單擊它。這些唯一的區別在於我嘗試過的跨文本,但它沒有解決。硒web驅動程序查找元素

List<WebElement> buttons=driver.findElements(By.className("ui-dialobuttonset")); 
buttons.get(0).click(). 

力工作

+0

當html源代碼不可用時很難得到原因。請張貼源代碼部分。否則,我只能建議你嘗試'By.xpath',因爲'By.className'不適合你。 – Quinn

回答

0

有一個名爲在第一按鈕UI狀態懸停類,這是不存在的按鈕2。所以你可以試試這個XPath:

driver.findElement(By.xpath("//button[contains(@class,'ui-state-hover')]")).click(); 
+0

嘗試過這種方式,但沒有運氣 – maddie

+0

最新的錯誤是什麼? –

0

classname你的發現是div,要查找按鈕的名稱列表按鈕名稱的按鈕即ui-button或者您可以通過tagName找到按鈕

List<WebElement> buttons=driver.findElements(By.className("ui-button")); buttons.get(0).click();

+0

沒有工作。我能夠使用xpath driver.findElement(By.xpath(「html/body/div [8]/div [3]/div/button [1]」))。click();但我不覺得所以這是尋找從Html標記開始的元素的最佳方法 – maddie

0

嘗試下面的代碼點擊直接的第一個按鈕..

確保使用正確的類名「類的名字是錯在你上面寫的代碼」

wd.findElement(By.xpath("//div[@class='ui-dialog-buttonset']/button[1]"))

如果它不工作請嘗試以下代碼

List<WebElement> buttons=wd.findElements(By.xpath("//div[@class='ui-dialog-buttonset']")); 
buttons.get(0).click(); 
相關問題