如何获取表格类型结构的下一个同级元素。

huangapple 未分类评论44阅读模式
英文:

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.

&lt;div class =&quot;table-class&quot;&gt;

&lt;table class =&quot;table-response&quot;&gt;

&lt;thead&gt;

&lt;tr&gt;

&lt;th class = &quot;ng-binding&quot; &gt;testcase&lt;/th&gt;

&lt;th class=&quot;ng-binding&quot;&gt; environment&lt;/th&gt;

&lt;th class=&quot;ng-binding&quot;&gt; source folder&lt;/th&gt;

&lt;/tr&gt;

&lt;/thead&gt;

&lt;tbody&gt;

&lt;tr class =&quot;ng-scope&quot;&gt;

&lt;td class=&quot;ng-input&quot;&gt;

&lt;input class=&quot;ng-scope-input&quot; id=&#39;testcaseid&quot;&gt;

&lt;/td&gt;

&lt;td class =&quot;ng-input&quot;&gt;

&lt;input class=&quot;ng-scope-input&quot; id=&quot;environmentid&quot;&gt;

&lt;/td&gt;

&lt;td class =&quot;ng-input&quot;&gt;

&lt;input class=&quot;ng-scope-input&quot; id=&quot;source folder&quot;&gt;

&lt;/td&gt;

&lt;/tr&gt;

&lt;/tbody&gt;

&lt;/table&gt;

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&lt;Webelement&gt; tlabels = BrowserFactory.getdriver().findElements(By.xPath(&quot;//div/table/thead/tr[@class=&#39;ng.binding&#39;]&quot;)
// here I will get two elements related to select in the list
List&lt;Webelement&gt; ltexts= BrowserFactory.getdriver().findElements(By.xPath(&quot;//div/table/tbody/tr/td/input[@class=&#39;ng-scope-input&#39;]&quot;)

for (Webelement label: tlabels )
{
if (label.getText().equals(&quot;TestCAse ID&quot;))
{
// 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(&quot;./following::input[&quot; + i + &quot;]&quot;))

Code:

List&lt;WebElement&gt; labels = driver.findElements(By.xpath(&quot;//table[@class=&#39;table-response&#39;]//tr//th&quot;));
		int i=1;
		for (WebElement label: labels )
		{
		if (label.getText().equals(&quot;environment&quot;))
		 {
		   label.findElement(By.xpath(&quot;./following::input[&quot; + i + &quot;]&quot;)).sendKeys(&quot;Environment&quot;);
		 }
		 i=i+1;
		}

huangapple
  • 本文由 发表于 2020年4月8日 20:57:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/61101239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定