Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/remote/shadowroot.py
1864 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
from hashlib import md5 as md5_hash
19
20
from ..common.by import By
21
from .command import Command
22
23
24
class ShadowRoot:
25
# TODO: We should look and see how we can create a search context like Java/.NET
26
27
def __init__(self, session, id_) -> None:
28
self.session = session
29
self._id = id_
30
31
def __eq__(self, other_shadowroot) -> bool:
32
return self._id == other_shadowroot._id
33
34
def __hash__(self) -> int:
35
return int(md5_hash(self._id.encode("utf-8")).hexdigest(), 16)
36
37
def __repr__(self) -> str:
38
return '<{0.__module__}.{0.__name__} (session="{1}", element="{2}")>'.format(
39
type(self), self.session.session_id, self._id
40
)
41
42
@property
43
def id(self) -> str:
44
return self._id
45
46
def find_element(self, by: str = By.ID, value: str = None):
47
"""Find an element inside a shadow root given a By strategy and
48
locator.
49
50
Parameters:
51
-----------
52
by : selenium.webdriver.common.by.By
53
The locating strategy to use. Default is `By.ID`. Supported values include:
54
- By.ID: Locate by element ID.
55
- By.NAME: Locate by the `name` attribute.
56
- By.XPATH: Locate by an XPath expression.
57
- By.CSS_SELECTOR: Locate by a CSS selector.
58
- By.CLASS_NAME: Locate by the `class` attribute.
59
- By.TAG_NAME: Locate by the tag name (e.g., "input", "button").
60
- By.LINK_TEXT: Locate a link element by its exact text.
61
- By.PARTIAL_LINK_TEXT: Locate a link element by partial text match.
62
- RelativeBy: Locate elements relative to a specified root element.
63
64
Example:
65
--------
66
element = driver.find_element(By.ID, 'foo')
67
68
Returns:
69
-------
70
WebElement
71
The first matching `WebElement` found on the page.
72
"""
73
if by == By.ID:
74
by = By.CSS_SELECTOR
75
value = f'[id="{value}"]'
76
elif by == By.CLASS_NAME:
77
by = By.CSS_SELECTOR
78
value = f".{value}"
79
elif by == By.NAME:
80
by = By.CSS_SELECTOR
81
value = f'[name="{value}"]'
82
83
return self._execute(Command.FIND_ELEMENT_FROM_SHADOW_ROOT, {"using": by, "value": value})["value"]
84
85
def find_elements(self, by: str = By.ID, value: str = None):
86
"""Find elements inside a shadow root given a By strategy and locator.
87
88
Parameters:
89
-----------
90
by : selenium.webdriver.common.by.By
91
The locating strategy to use. Default is `By.ID`. Supported values include:
92
- By.ID: Locate by element ID.
93
- By.NAME: Locate by the `name` attribute.
94
- By.XPATH: Locate by an XPath expression.
95
- By.CSS_SELECTOR: Locate by a CSS selector.
96
- By.CLASS_NAME: Locate by the `class` attribute.
97
- By.TAG_NAME: Locate by the tag name (e.g., "input", "button").
98
- By.LINK_TEXT: Locate a link element by its exact text.
99
- By.PARTIAL_LINK_TEXT: Locate a link element by partial text match.
100
- RelativeBy: Locate elements relative to a specified root element.
101
102
Example:
103
--------
104
element = driver.find_elements(By.ID, 'foo')
105
106
Returns:
107
-------
108
List[WebElement]
109
list of `WebElements` matching locator strategy found on the page.
110
"""
111
if by == By.ID:
112
by = By.CSS_SELECTOR
113
value = f'[id="{value}"]'
114
elif by == By.CLASS_NAME:
115
by = By.CSS_SELECTOR
116
value = f".{value}"
117
elif by == By.NAME:
118
by = By.CSS_SELECTOR
119
value = f'[name="{value}"]'
120
121
return self._execute(Command.FIND_ELEMENTS_FROM_SHADOW_ROOT, {"using": by, "value": value})["value"]
122
123
# Private Methods
124
def _execute(self, command, params=None):
125
"""Executes a command against the underlying HTML element.
126
127
Args:
128
command: The name of the command to _execute as a string.
129
params: A dictionary of named parameters to send with the command.
130
131
Returns:
132
The command's JSON response loaded into a dictionary object.
133
"""
134
if not params:
135
params = {}
136
params["shadowId"] = self._id
137
return self.session.execute(command, params)
138
139