0

下面是兩個複選框的html,我的目的是選擇一個複選框,使用它的標籤作爲'XERU'或'SYM'。在Robot Framework中,關鍵字'應該選擇複選框'使用鍵屬性; ID和名稱,因爲它是定位器,但這裏的ID對每個標籤都是動態的。無法爲動態輸入ID值選擇複選框

因此,我需要爲複選框選擇Xpath作爲ID /名稱,但不要在測試套件中進行硬編碼。

<input id="ctl00_PageBody_chklistVirtualAddressingGroups_5" name="ctl00$PageBody$chklistVirtualAddressingGroups$5" type="checkbox"/> 
<label for="ctl00_PageBody_chklistVirtualAddressingGroups_5">XERU</label> 
<br/> 
<input id="ctl00_PageBody_chklistVirtualAddressingGroups_6" name="ctl00$PageBody$chklistVirtualAddressingGroups$6" type="checkbox"/> 
<label for="ctl00_PageBody_chklistVirtualAddressingGroups_6">SYM</label> 
<br/> 

回答

1

您可以嘗試下面的代碼。此代碼基於您提供的HTML。請嘗試提供更多信息。

driver.findElement(By.xpath("//label[contains(text(), 'XERU')]")).click(); 
driver.findElement(By.xpath("//label[contains(text(), 'SYM')]")).click(); 
+0

我使用機器人框架執行,你的答案適用於硒@Maninder。 –

+0

如果這個答案解決了你的問題,你可以把它標記爲「接受」@MuddesirAhmed – Goralight

0

我想嘗試的東西沿着這些路線:

Wait Until Keyword Succeeds 4 sec 1 sec Click Element xpath=//label[contains(text(), 'XERU')] 
Wait Until Keyword Succeeds 4 sec 1 sec Click Element xpath=//label[contains(text(), 'SYM')] 

如果失敗,你可以給我們更多的信息,爲什麼有問題?作爲

+0

這就是說,爲什麼你不做一個初始階段來簡單地找到你需要的元素的ID,然後從那裏使用checkbox關鍵字?也許更穩定和可維護 – shicky

0

使用的XPath -

(//label[text()='XERU']/preceding::input)[last()] 
(//label[text()='SYM']/preceding::input)[last()]