我一直在努力从Table获取文本。当我检查firebug时,我可以从xpath获取值,但是当我运行代码时,它不会获取值。 该表如下 -
div class="description__table-holder">
<table class="description__table">
<tbody>
<tr>
<td>Test: </td>
<td>9780238905</td>
<td>Pas: </td>
<td>600</td>
</tr>
<tr>
<td>Size: </td>
<td>216 x 274mm</td>
<td> Result: </td>
<td> 9 To 12 </td>
</tr>
</tbody>
</table>
</div>
我尝试过以下Xpath -
html/body/div[2]/div[5]/div[2]/div/article/section[1]/section[2]/div[4]/div[1]/article/div/table/tbody/tr[1]/td[1]
html/body/div[2]/div[5]/div[2]/div/article/section[1]/section[2]/div[4]//table[@class='description__table']/tbody/tr[1]
html/body/div[2]/div[5]/div[2]/div/article/section[1]/section[2]/div[4]/div[1]/article/div/table/tbody/tr[1]/td/following-sibling::td[3]
但是当我尝试在selenium / c#/ phantomJS驱动程序中获取值时,我无法.Below是我的代码 -
var datanew = driver.FindElements(By.XPath("html/body/div[2]/div[5]/div[2]/div/article/section[1]/section[2]/div[4]//table[@class='description__table']/tbody/tr[1]"));
var other = "";
ArrayList others = new ArrayList();
foreach (var dat in datanew)
{
//var linkDriver = new FirefoxDriver();
other = dat.Text;
others.Add(other);
}
string test = driver.FindElement(By.XPath("html/body/div[2]/div[5]/div[2]/div/article/section[1]/section[2]/div[4]/div[1]/article/div/table/tbody/tr[1]/td/following-sibling::td[3]")).Text;
答案 0 :(得分:0)
尝试给予一些等待&amp;检查
Thread.sleep(15);
string test = driver.FindElement(By.XPath("html/body/div[2]/div[5]/div[2]/div/article/section[1]/section[2]/div[4]/div[1]/article/div/table/tbody/tr[1]/td/following-sibling::td[3]")).Text;
如果你没有在你的程序中提供隐式或显式等待,那么有些xpath可能会失败,即使它可能在UI上正常工作。这是因为某些元素在UI上加载需要时间,因此无法找到元素和元素。脚本失败了。另请尝试使用explicit-wait代码,这是更好的方法,我建议您使用relative xpath instead of absolute xpath