Home > database >  Python Selenium - Constantly changing web element ID
Python Selenium - Constantly changing web element ID

Time:10-21

I'm trying to use Selenium to find a web element that I know is the To field in a web email application (please see picture). I am able to successfully identify this web element and use send_keys to send an email address to this field.

However, the issue is the id always seems to cycle between 299 and 3 other numbers like 359 or 369. Here's the code Im using. Is there another way I can account for this changing ID?

to_field = wait.until(EC.element_to_be_clickable((By.ID, "v299-to-input")))
print(to_field)
to_field.send_keys(email_reciever)

enter image description here

Thanks

PS- The web email application is fastmail.com

CodePudding user response:

Since the ID attribute is changing, you have to build your locator on some other, stable parameter. The following CSS Selector is stable:

to_field = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".s-compose-to textarea")))
print(to_field)
to_field.send_keys(email_reciever)

The same can be done with the use of XPath locator:

to_field = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class,'s-compose-to')]//textarea")))
print(to_field)
to_field.send_keys(email_reciever)

CodePudding user response:

You can use following css selector to identify the element.

to_field = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea[id$='-to-input']")))
print(to_field)
to_field.send_keys(email_reciever)

This will identify the element id ends with the -to-input static value

to_field = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea.v-EmailInput-input")))
print(to_field)
to_field.send_keys(email_reciever)

This will identify the element with tagname.classname

  • Related