英文:
How to get the following-sibling element for a table type of structure
问题
public void addInputs() {
List<Webelement> tlabels = BrowserFactory.getdriver().findElements(By.xPath("//div/table/thead/tr[@class='ng-binding']"));
List<Webelement> ltexts = BrowserFactory.getdriver().findElements(By.xPath("//div/table/tbody/tr/td/input[@class='ng-scope-input']"));
for (Webelement label : tlabels) {
if (label.getText().equals("TestCAse ID")) {
Webelement siblingInput = label.findElement(By.xpath("following-sibling::th/input[@class='ng-scope-input']"));
siblingInput.sendKeys("your_text_here");
}
}
}
注:上面的代码翻译了你提供的代码片段,用于查找表格中的特定表头,然后在对应的文本框中输入文本。由于你要求只返回翻译后的部分,我已经删除了其他内容。如果你有任何进一步的问题或需要更多帮助,请随时提问。
英文:
On the same note for a table kind of Element structure..as given below, how to apply the following-sibling . If the table hearder matches with the input string, enter the value in its corresponding text box.
<div class ="table-class">
<table class ="table-response">
<thead>
<tr>
<th class = "ng-binding" >testcase</th>
<th class="ng-binding"> environment</th>
<th class="ng-binding"> source folder</th>
</tr>
</thead>
<tbody>
<tr class ="ng-scope">
<td class="ng-input">
<input class="ng-scope-input" id='testcaseid">
</td>
<td class ="ng-input">
<input class="ng-scope-input" id="environmentid">
</td>
<td class ="ng-input">
<input class="ng-scope-input" id="source folder">
</td>
</tr>
</tbody>
</table>
TIn UI, this will be exactly in below format:
//Table Header
TestCAse ID Environement Source folder
<text box> <text box> <Text box>
public void add inputs(){
// here I will get two elements int the list
List<Webelement> tlabels = BrowserFactory.getdriver().findElements(By.xPath("//div/table/thead/tr[@class='ng.binding']")
// here I will get two elements related to select in the list
List<Webelement> ltexts= BrowserFactory.getdriver().findElements(By.xPath("//div/table/tbody/tr/td/input[@class='ng-scope-input']")
for (Webelement label: tlabels )
{
if (label.getText().equals("TestCAse ID"))
{
// here I have to enter the string . //need help how we can use following-sibling for table type of structure.
}
}
}
with reference to :
https://stackoverflow.com/questions/61097750/for-a-parent-element-there-are-two-child-elements-if-text-of-child-1-matche
答案1
得分: 1
我建议实现这样的场景初始化数字i=1
,然后在迭代时增加该数字以找到元素的实际计数,并在使用时传递该值
element.findElement(By.xpath(" ./following::input[" + i + "]"))
代码:
List<WebElement> labels = driver.findElements(By.xpath("//table[@class='table-response']//tr//th"));
int i=1;
for (WebElement label: labels )
{
if (label.getText().equals("environment"))
{
label.findElement(By.xpath(" ./following::input[" + i + "]")).sendKeys("Environment");
}
i=i+1;
}
英文:
I would suggest to implement such scenario initialize number i=1
and then while iterating increase the number to find the actual count of the element and pass that value while using
element.findElement(By.xpath("./following::input[" + i + "]"))
Code:
List<WebElement> labels = driver.findElements(By.xpath("//table[@class='table-response']//tr//th"));
int i=1;
for (WebElement label: labels )
{
if (label.getText().equals("environment"))
{
label.findElement(By.xpath("./following::input[" + i + "]")).sendKeys("Environment");
}
i=i+1;
}
专注分享java语言的经验与见解,让所有开发者获益!
评论