Trendo
Trendo

Reputation: 3

How can i select element in selenium webdriver by part of ID.

I have a td element with ID like "a:2:3:d:", and when i want to select it by id, my webdriver can not find it. Is there a way to search by part of ID, because I think, that the problem is at last ":" in the identifier.

Upvotes: 0

Views: 1306

Answers (2)

Purus
Purus

Reputation: 5799

Assuming you are using Java

WebElement el = driver.findElement(By.cssSelector("td[id*='a:2:3']"));

The above code gets the element of TD which starts with a:2:3 as we use * in the css Selector.

XPath is more powerful and can be sometimes difficult to understand. CSS Selector is easy.

Upvotes: 0

DNA
DNA

Reputation: 42597

First, you need to confirm that this really is the problem, and it's not just that the page isn't fully loaded, or is loaded asynchronously. I don't see any particular reason why Selenium should care about the trailing ":".

Update: From the comments, it's much more likely that the dynamic id that is the problem, but the solution is the same either way:

To find an element by partial id, you can use xpath. If you were trying to find a div by partial id, for example:

//div[contains(@id, 'a:2:3')]

You don't say what language you are using, but in python, this would be used as follows:

driver.find_element_by_xpath("//div[contains(@id, 'a:2:3')]")

and in Java:

driver.findElement(By.xpath("//div[contains(@id, 'a:2:3')]"))

Upvotes: 2

Related Questions