我在Java中使用Webdriver,我需要等待具有动态ID的元素显示在网页上 .
例如,我有隐式等待的以下语句:WebDriverWait wait = new WebDriverWait(driver,30); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id( “dynamicID”)))
dynamicID
是's ID that I am waiting for, and normally it comes with a number after which I can' t真正预测的元素的前缀 .
这里有一些很好的答案Finding an element by partial id with Selenium in C# .
我试图改变我的代码
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(^="dynamicID")))
它不起作用 .
我试图使用页面工厂:
@FindBy(id ^= "dynamicID")
private WebElement something;
它也不起作用 .
所以我不得不提问:1 . 如何在隐式等待语句中使用ID部分匹配? 2.如果我正在使用页面工厂,并假设@FindBy正在工作,我如何在隐式等待中使用此元素 something
?
wait.until(ExpectedConditions.visibilityOfElementLocated(something))
不起作用 .
首先十分感谢 .
2 回答
您可以使用By.xpath,并使用xpath
starts-with
函数,如下所示:*是表示任何元素类型的wild字符 . 如果您知道标签,则可以替换它 . 例如,如果它是div:
您可以对
id
使用部分匹配,但必须使用xpath
或css
选择器 .tag
是元素的标签 .