Reputation: 405
I am using Webdriver in Java and I need to wait for an element which has a dynamic ID to show up on the web page.
For example, I have the following statement for the implicit wait: WebDriverWait wait = new WebDriverWait(driver, 30); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicID")))
dynamicID
is the prefix of the element's ID that I am waiting for, and normally it comes with a number after which I can't really predict.
There was some good answers here Finding an element by partial id with Selenium in C#.
I tried to change my code to be
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(^="dynamicID")))
and it is not working.
I tried to use the page factory:
@FindBy(id ^= "dynamicID")
private WebElement something;
and it is not working too.
So I have to questions:
1. how do I use ID partial match in my implicit wait statement?
2. if I am using the page factory, and assume the @FindBy is working, how do I use this element something
in the implicit wait?
wait.until(ExpectedConditions.visibilityOfElementLocated(something))
does not work.
Thanks very much in advance.
Upvotes: 1
Views: 1578
Reputation: 8274
You can use By.xpath, and use the xpath starts-with
function, like so:
wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//*[starts-with(@id, 'dynamicID')]")));
The * is a wild character representing any element type. If you know the tag, you can replace it. For example, if it's a div:
wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//div[starts-with(@id, 'dynamicID')]")));
Upvotes: 1
Reputation: 5453
You can use a partial match for the id
but you have to use either an xpath
or css
selector.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.CssSelector("tag[id^='dynamicID']")))
Where tag
is the element's tag.
Upvotes: 0