I have a link inside a table with a dynamically changing id, the xpath being: //*[@id="1544727471470-1-uiGrid-0007-cell"]/a/span/span

To its left is a checkbox with xpath: //*[@id="1544727471470-1-uiGrid-0007-cell"]/input

As I go down the table, the string after the dynamic id ('1') gets implemented, and so the fifth row would have the xpath: //*[@id="1544727471470-5-uiGrid-0007-cell"]/a/span/span

I can infer that the checkbox next to it will be: //*[@id="1544727471470-5-uiGrid-0007-cell"]/input

So, you see the pattern. What I need to do is capture the dynamic id ("1544727471470"), and from there, I can write logic to create my xpaths and target any row in view and click on its checkbox.

I tried looking at page source, but shows nothing about the dynamic ids.


1 Answer 1


I think you could use the contains() xpath function or the *=, ^=, $= family of CSS selectors to look up the desired element with the partial match on the id attribute and then extract the id attribute to get the dynamically changing part.

Something along these lines:

element_with_the_dynamic_id = driver.find_element_by_css_selector('[id$="uiGrid-0007-cell"]')
dynamic_id = element_with_the_dynamic_id.get_attribute("id")

dynamic_part = dynamic_id.split("-", 1)[0]

$= here means "ends with"; .split("-", 1) splits by the first occurence of -.

If you prefer XPaths, you could use contains() (ends-with() unfortunately is XPath 2.0 only):

element_with_the_dynamic_id = driver.find_element_by_xpath('//*[contains(@id, "uiGrid-0007-cell")]')
  • Your solution worked perfectly! The get_attribute was the key step for me. Thank you very much!
    – Angel Joy
    Dec 14, 2018 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.