[英]select checkbox using selenium webdriver
我需要選擇具有相同ID HTML的復選框:
<label class="table-checkbox-label" for="record-12034">
<input id="record-12034" class="table-checkbox" type="checkbox"/>
</label>
</td>
<td>91363007</td>
<td>EC4N</td>
<td>true</td>
<td>ACTIVE</td>
</tr>
<tr data-id="12201">
<td>
<label class="table-checkbox-label" for="record-12201">
<input id="record-12201" class="table-checkbox" type="checkbox"/>
</label>
</td>
復選框列表 。 我會將合同ID發送到合同ID下方的filterbox,就像自動完成框一樣,因此它將根據輸入給出結果。 像這樣,但是這里我的硒代碼是單擊復選框列表中的第一個復選框,如圖1所示。
請任何建議
編輯::關鍵字功能的代碼:
public void filter(String objectName,String testData) throws InterruptedException,IOException {
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input")));//wait for textbox
driver.findElement(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input")).sendKeys(testData);//send data to textbox
System.out.print("Text box is now visible");
driver.findElement(By.xpath(".//*[starts-with(@id,'record')]")).click();//click on checkbox
driver.findElement(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input")).clear();//clear textbox
}
讀取Excel文件的代碼:
public void ASRTaccounts() throws IOException, InterruptedException, EncryptedDocumentException, InvalidFormatException, AWTException {
keyword = new keywords();
List<String> data = new ArrayList<String>();
File file = new File("C:\\RDMT test\\rdmt_test\\RDMT_\\LeadSuite.xlsx");
Workbook workbook = WorkbookFactory.create(file);
DataFormatter formatter = new DataFormatter();
Sheet sheet = workbook.getSheet("login");
for (Row row : sheet) {
for (Cell cell : row) {
data.add(formatter.formatCellValue(cell));
}
}
System.out.println(data);
for (int i=3;i<data.size();i++){
if (data.get(i).equals("filter")){
String key = (String) data.get(i);
String testData = (String) data.get(i+1);
String objectName = (String) data.get(i+2);
System.out.println(key);
System.out.println(testData);
System.out.println(objectName);
keyword.filter(objectName,testData);
}
問題在於選擇要單擊的復選框。
driver.findElement(By.xpath(".//*[starts-with(@id,'record')]")).click();
這將查找所有ID中帶有“記錄”的元素(在您的案例中都是復選框),並且僅返回第一個。 您需要使用類似該driver.findElement(By.xpath(".//input[@id='record-idofcheckboxyouwanttoclick']")).click();
東西來選擇要單擊的確切一個復選框driver.findElement(By.xpath(".//input[@id='record-idofcheckboxyouwanttoclick']")).click();
編輯:哦,現在我知道問題出在哪里了。 不確定過濾后的HTML外觀如何,但應使用xpath查找包含發送的輸入/數字的<td>
元素,然后找到其<label>
包含所需的<input>
(復選框)的同級元素點擊。
使用前面的 xpath函數可以選擇文本框之前的復選框。 如果您仍然遇到相同的問題,請嘗試關注並讓我知道:
public void filter(String objectName,String testData) throws InterruptedException,IOException {
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input")));//wait for textbox
driver.findElement(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input")).sendKeys(testData);//send data to textbox
System.out.print("Text box is now visible");
driver.findElement(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input/preceding::input[1]")).click();//click on checkbox
driver.findElement(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input/preceding::input[1]")).clear();//clear textbox
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.