我刚开始使用selenium c#。我有一个树视图,它的位置是不一致的,所以我想要扩展它,如果它没有被扩展,如果它已经被扩展了,它应该是原样的。崩塌也是如此。
这是树展开的时候。
<div class="rtTop">
<span class="rtSp"></span>
<span class="rtMinus"></span>
<span class="rtIn">Roles</span>
</div>这就是它崩溃的时候
<div class="rtTop">
<span class="rtSp"></span>
<span class="rtPlus"></span>
<span class="rtIn">Roles</span>
</div>我目前正在使用
public static string treeviewExpand( string treeviewExpandButton)
{
treeviewExpandButton = "//span[text()='" + treeviewExpandButton + "']/preceding-sibling::span[@class='rtPlus']";
return treeviewExpandButton;
}
public static string treeviewCollapse(string treeviewCollapseButton)
{
treeviewCollapseButton = "//span[text()='" + treeviewCollapseButton + "']/preceding-sibling::span[@class='rtMinus']";
return treeviewCollapseButton;
}如果调用适当的操作,上面的xpath可以正常工作。但是我想要一个通用的操作函数来扩展和折叠树,而不管它的当前状态如何。
我试图使用文本获取treeview节点的当前类名。在这里,我试图获取当前类"rtPlus“或"rtMinus”,但是当我试图使用标记名作为span获取前面的同级时,我是使用类"rtsp“而不是类"rtPlus”或"rtMinus“来获得标记跨度,即使检查显示其前面的同级跨度有类”rtPlus“或"rtMinus”。
我在用
public static string treeviewExpandCollapse(string treetext)
{
treetext= "//span[text()='" + treetext+ "']";
IWebElement element;
element = driver.FindElement(treetext).FindElement(By.XPath("./preceding-sibling::span"));
string calsss = element.GetAttribute("class");
Thread.Sleep(2000);
}发布于 2017-06-21 16:58:56
XPaths可能很棘手,特别是当您开始将多个FindElement和XPaths链接在一起时。对于您的场景,我只需使用一个XPath就可以保持简单:
"//span[text()='" + text + "']/parent::div/span[2]"解释:
//span[text()='" + text + "'] -使用给定的text 选择<span><span class="rtIn">Roles</span>
/parent::div -选择<span>的父<div class="rtTop">
/span[2] -在父级中,在索引2处选择<span> (无论<span>是展开还是折叠) <span class="rtPlus"></span>
代码:
IWebElement element = driver.FindElement(
By.XPath("//span[text()='" + text + "']/parent::div/span[2]"));https://stackoverflow.com/questions/44677564
复制相似问题