问题
I recently recommended to my devs to add IDs to each element on the project I'm working to make automation more robust, they added in aria-uuid to each element. I cannot get anything to recognize these IDs! I'm wondering if it is even possible?
I'm using python/selenium.
I've tried identifying elements by ID, I've done CSS selectors and xpaths but they have had a history of breaking between new builds.
Relevant html:
input class="short ng-valid ng-not-empty ng-valid-min ng-valid-required" name="question_16" type="number" aria-uuid="question_16_input" ng-required="true" ng-min="0" ng-model="$ctrl.vault['question_16'].value"
def click_element_by_id(self, driver_init, id1, message1, delay1, halt):
try:
element = WebDriverWait(driver_init, delay1).until(EC.element_to_be_clickable((By.ID, id1)))
element.click()
except TimeoutException:
if halt:
assert_that(True, message1).is_false()
else:
print(message1)
Each time I get the assertion/timeout error
回答1:
Ideally, yes you should have been able to recognize each individual elements with respect to their aria-uuid
to be used with Selenium provided the generated aria-uuid
were static.
As per the HTML you have shared the generated aria-uuid
seems to be dynamic. So aria-uuid
alone won't help you. In these cases you have to use the aria-uuid
along with the other attributes
to uniquely identify the elements. To identify this element you can use either of the following Locator Strategies:
Using
CSS_SELECTOR
:WebDriverWait(driver_init, delay1).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.short.ng-valid.ng-not-empty.ng-valid-min.ng-valid-required[aria-uuid$='_input'][name^='question_']"))).click()
Using
XPATH
:WebDriverWait(driver_init, delay1).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='short ng-valid ng-not-empty ng-valid-min ng-valid-required' and contains(@aria-uuid, '_input')][starts-with(@name, 'question_')]"))).click()
回答2:
It should be possible with a CSS selector [aria-uuid='question_16_input']
来源:https://stackoverflow.com/questions/57592158/can-selenium-recognize-aria-uuid-as-an-id-for-object-recognition