[英]Selenium Webdriver Java: How to click on a particular cell in table using row and column numbers
[英]How to click on the aside / previous Table Cell (TD) in the same row using Selenium Webdriver with Java?
我想單擊動態 web 表中存在的 Checkbox 元素,該表具有 3 個 static 列(復選框、描述、鏈接)和動態行。
這是我試圖達到我的期望但沒有奏效的腳本。 可能是錯誤的做法。
WebElement dataTable = driver.findElement(By.id("table_id"));
List<WebElement> TDs = dataTable.findElements(By.tagName("td"));
for(WebElement td : TDs)
{
if (!td.getText().trim().equals("text that i want to click on its checkbox"))
continue;
WebElement particularTd = td.findElement(By.xpath("//*[@type='checkbox']//preceding::input"));
particularTd.click();
}
你能告訴我點擊復選框的正確方法嗎?
謝謝,
卡魯納加拉潘迪 G
我認為 td 只包含數據。 您可以嘗試的是導航回包含表數據(td)的直接父表行(tr)。 然后在那里搜索輸入“復選框”。
<table>
<tr>
<td>
<center><input type='checkbox'/></center>
</td>
<td>
<span>Cell Desc</span>
</td>
<td>
<hyperlink>
</td>
</tr>
</table>
要單擊復選框,您可以使用 xpath:
//*[@text='Cell Desc']/ancestor::tr//input
要使 xpath 動態化,您可以獲取該值並使用該值
String value = "Cell Desc";
String xpath = "//*[@text='" + value + "']/ancestor::tr//input";
This worked for me please try the below code:
for(WebElement td : TDs)
{
if (td.getText().contains("Your Text")
td.click;
}
//if you are unable to click with td.click; use below code
for(WebElement td : TDs)
{
if (td.getText().contains("Your Text")
{
WebElement particularTd = td.findElement(By.xpath("//*[@type='checkbox']//preceding::input"));
particularTd.click()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.