Locating and Find Element with dynamic ID using selenium in Pyhton

Question:

How to find and select an element that has a dynamic ID, that always changing every reloading the page,
this is the ID i want to select id=x-auto-234-input but the 3 digit number always changing, Any ideas?

Below is the script

<div role="presentation" class="MegaEntryTextField x-component" id="x-auto-234" style="width: 60px; height: 22px;">
  <input type="text" class=" x-form-field x-form-text " id="x-auto-234-input" tabindex="0" style="width: 52px; height: 18px;">
</div>

enter image description here

Can you suggest XPATH and CSS selector that work

Asked By: bisaso masaso

||

Answers:

From this limited piece of code, I think the most specific you can get would be this CSS selector:

div.MegaEntryTextField.x-component[id^="x-auto-"][role="presentation"]>input[id^="x-auto-"][id$="-input"][type="text"]

The main part here is[id^="x-auto-"][id$="-input"], which only will select an element with an ID that starts with x-auto- and ends with -input, effectively only leaving out the dynamic number. The rest of the selector matches classes, tag names, and attributes, just for good measure.

Answered By: Michael M.

Here is the solution:

driver.find_element(By.CSS_SELECTOR, 'input[id^="x-auto-"]')

Please refer here for more details

Answered By: Ajeet Verma