user3638751
user3638751

Reputation: 113

How to handle webelement's id - not attribute id(!) in javascript from selenium-python?

I need to handle webelements by identifying them uniquely. Attribute id has known limitations: it might be missing and it might be non-unique. There is a possibility to use evaluate, and I'm interested whether it's possible to identify elements by their id - not attribute id?

Please consider the following code: I search element by its attribute id and then print its id - they are absolutely different

> Blockquote
>>> elem=driver.find_element(‘id’, ‘userInfoWhoRadioGroup_errorMessage’)
>>> elem.id
‘0ce5ef48-df80-487f-ada2-fc999dedb614’
>>> e.get_attribute(‘id’)
‘userInfoWhoRadioGroup_errorMessage’
>>>
> Blockquote
The beauty of the element's id is that every element has it and that it's unique.

Some more details: the elements I need to handle are already found using webdriver, I have them and their ids. Now I'd like to run javascript on them (without refreshing the page that would change the ids). And I'm looking for a way to uniquely identify them by these ids

Upvotes: 0

Views: 138

Answers (1)

supputuri
supputuri

Reputation: 14135

HTML attribute id and elem.id are completely of 2 different things. The HTML id is specified in the HTML, where was elem.id is the id assigned to the element by selenium. This will be always unique and also changes when the page is refreshed/reloaded.

I would not recommend using elem.id for identification purpose.

You can interact with the element using the below

driver.execute_script("arguments[0].textContent", elem)

Upvotes: 1

Related Questions