问题
In the specs it seems to state that element IDs are meant to be numbers. However, both Firefox and Chrome (in w3c mode) return something like c87e08da-358e-45a8-b75c-c17cde92f606
. Chrome returns something like 45.32131231423424324324254245
when NOT in w3c mode.
So... are they meant to be numbers? Or, are those strings representing numbers? How do I cast them back to Integer?
回答1:
As you mentioned element IDs are meant to be numbers
, straight answer is No.
As Firefox (being W3C compliant) returns something like c87e08da-358e-45a8-b75c-c17cde92f606 is a UUID (Universally Unique IDentifier
) is also known as GUID (Globally Unique IDentifier
) is the standardized format as per the W3C WebDriver Specs to identify an element following the RFC4122
UUID
An UUID
is an identifier which is unique across both space and time, with respect to the space of all UUIDs
. Hence an UUID
can be used for multiple purposes, from tagging objects with an extremely short lifetime, to reliably identifying very persistent objects across a network.
The internal representation of a UUID
is a specific sequence of bits in memory. To accurately represent a UUID as a URN, it is necessary to convert the bit sequence to a string representation. Each field is treated as an integer and has its value printed as a zero-filled hexadecimal digit string with the most significant digit first.
Example
A typical string representation of a UUID
is : uuid
:
f81d4fae-7dec-11d0-a765-00a0c91e6bf6
Generating UUIDs
Three algorithms are generally used to maintain uniqueness of generated UUIDs
:
- An algorithm that leverages the unique values of IEEE 802 MAC addresses which are available on network-connected hosts.
- The second algorithm uses pseudo-random number generators.
- The third uses cryptographic hashing and application-specific text strings.
Hence, the UUIDs
generated according to these mechanisms will be always unique from all other UUIDs
that have been or will be assigned and no question of casting
them back to Integer till the WebDriver
variants successfully negotiates the UUIDs
with the respective pair of Browsers
.
回答2:
You are confusing two different things. The spec is referring to a number that is an index into the collection of frames on the page. Your examples are GUIDs that are just unique identifiers of referenced elements on the page. The two are not the same nor interchangeable.
From the link you provided (under #5),
id is a Number object
This is in the context of using Switch To Frame. This allows the API to accept a number (which is an index into the collection of frames) to access a frame. For example, in Java you would use
driver.switchTo().frame(0)
where 0
is the id
which is a "number object" which is referred to in the portion of the spec you referenced. This would switch the driver context into the first (0th) frame on the page. There are other ways to switch to a frame, e.g. using a known web element. This is the third method listed in the spec (also under #5)
id represents a web element
The spec then defines how to handle the various cases for a web element.
The numbers that you refer to in your question are just GUIDs or unique identifiers to currently referenced web elements as FlorentB described in comments. You can't pass these numbers/GUIDs into the API to switch frames, etc.
If you want to see how this is implemented in Java, you can see the docs here.
来源:https://stackoverflow.com/questions/48404087/are-element-ids-numbers-in-webdrivers