bisaso masaso
bisaso masaso

Reputation: 13

Locating and Find Element with dynamic ID using selenium in Pyhton

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

Upvotes: 1

Views: 327

Answers (2)

Ajeet Verma
Ajeet Verma

Reputation: 3081

Here is the solution:

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

Please refer here for more details

Upvotes: 0

Michael M.
Michael M.

Reputation: 11090

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.

Upvotes: 0

Related Questions