我已经生成了具有这种结构的生成网页:
div
...
div
div
div class=x_1
<span randomtag=y> text1 </span>
div class=x
<span randomtag=z> text2 </span>
div class=x
<span randomtag=q> text3 </span>
div
div
div
...
div
我想点击randomtag = z。我试过了:
的xpath:
的CSS:
以下不起作用
css=div > span[randomtag="z"]
css=div.x span[randomtag="z"]
我有点失去了选择正确元素的可能性。
机器人代码是基本的 等到元素可见|定位器(无论它是什么) 例如
wait until element is visible css=div.x span[randomtag="z"]
wait until element is visible xpath=<previous path here>/div[2]/div[2]
有什么想法吗?这是反应的原因吗?
我得到“元素在x秒内不可见”。对于超时,我尝试了5-30秒。
编辑:
我还尝试使用搜索工具(浏览器上的dev工具后面)找到xpath,并在搜索时插入xpath或css定位器时找到该元素。
答案 0 :(得分:0)
public static void waitUntilElementIsVisible(WebElement element, WebDriver driver)
{
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
wait.pollingEvery(250, TimeUnit.MILLISECONDS);
wait.withTimeout(2, TimeUnit.MINUTES);
wait.ignoring(ElementNotVisibleException.class); //make sure that this exception is ignored
Function<WebDriver, WebElement> function = new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
System.out.println("Checking for the element!!");
if(element.isDisplayed() != true)
{
System.out.println("Target element is not visible");
}
return element;
}
};
wait.until(function);
}
然后打电话给:
WebDriver el = driver.FindElement(By.css("yourcss"));
waitUntilElementIsVisible(el, driver);