【问题标题】:How to select the previous column of a table based on the text of the next column using selenium webdriver?如何使用 selenium webdriver 根据下一列的文本选择表格的上一列?
【发布时间】:2016-08-27 14:08:30
【问题描述】:

我正在尝试根据下一列的文本选择 webtable 的上一列。这是我的代码:

for(WebElement eachrow:noofrows)
{
     List<WebElement> col3=eachrow.findElements(By.xpath("//td[3]")); 

     for(WebElement text:col3)  
     {
        String s1=text.getText();
        System.out.println(s1);
        if(s1==opt1)
        {
            driver.findElement(By.xpath("//td[2]")).click();
        }
     }

     if(t.equals(choice1)||t.equals(choice2))
     {
        text.findElement(By.xpath("//td[2]")).click();
     }

这里是html:

style="display: table-cell; vertical-align: top; -moz-user-select: none;">

<div class="ib10_checklist_question">An angel investor is the best choice. Why? Select up to two reasons. This is the appropriate funding amount for an angel investor. Angel investors are the best choice for backing an idea to get off the ground. Angel investors are backed by churches. Angel investors have limitless funds. </div>
<div class="ib10_checklist_bg">




<table style="width:100%">
<tbody>
<tr style="height:10%">
<td style="width:5%"/>
<td style="width:5%">
  <div class="ibg_checkbox" onclick="if (c2_callFunction) c2_callFunction('lvl2_check',[0]);">b</div>
</td>
<td class="ib10_checklist_text" style="width:100%">This is the appropriate funding amount for an angel investor.</td>
</tr>

【问题讨论】:

  • html 代码作为文本添加到您的问题中,而不是附加图像!还要澄清哪个是目标元素,并在出现任何错误时指定错误。谢谢
  • 根据谢谢

标签: selenium dom html-table


【解决方案1】:

Xpath 通过使用&lt;td class="ib10_checklist_text----&gt;的文本

//*[text()='This is the appropriate funding amount for an angel investor.']/preceding-sibling::td[1]/div

Xpath 通过使用&lt;td class="ib10_checklist_text----&gt; 的类值

 //td[@class='ib10_checklist_text']/preceding-sibling::td[1]/div

如果您想进一步探索它,那么这个video 将对您有用。

【讨论】:

  • 请投票并接受答案以保持卫生:) 谢谢!
猜你喜欢
  • 2020-05-20
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多