1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! FrameLocator for locating elements inside iframes.
//!
//! FrameLocator is a lightweight client-side object (like Locator) that represents
//! a view to an iframe on the page. It uses Playwright's `internal:control=enter-frame`
//! selector engine to cross iframe boundaries transparently.
//!
//! # Example
//!
//! ```ignore
//! use playwright_rs::Playwright;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let playwright = Playwright::launch().await?;
//! let browser = playwright.chromium().launch().await?;
//! let page = browser.new_page().await?;
//!
//! page.goto("https://example.com", None).await?;
//!
//! // Locate elements inside an iframe
//! let frame = page.frame_locator("iframe#my-frame").await;
//! frame.locator("button").click(None).await?;
//!
//! // Use get_by_* methods inside the iframe
//! frame.get_by_text("Submit", false).click(None).await?;
//!
//! // Nested iframes
//! let inner = frame.frame_locator("iframe.nested");
//! inner.locator("h1").text_content().await?;
//!
//! browser.close().await?;
//! Ok(())
//! }
//! ```
//!
//! See: <https://playwright.dev/docs/api/class-framelocator>
use crate::protocol::locator::{
get_by_alt_text_selector, get_by_label_selector, get_by_placeholder_selector,
get_by_role_selector, get_by_test_id_selector, get_by_text_selector, get_by_title_selector,
};
use crate::protocol::{AriaRole, Frame, GetByRoleOptions, Locator, Page};
use std::sync::Arc;
/// FrameLocator represents a view to an iframe on the page.
///
/// It is used to locate elements inside iframes. FrameLocator is not a
/// protocol object — it builds selector strings that include the special
/// `internal:control=enter-frame` directive, which the Playwright server's
/// selector engine understands to cross iframe boundaries.
///
/// See: <https://playwright.dev/docs/api/class-framelocator>
#[derive(Clone)]
pub struct FrameLocator {
frame: Arc<Frame>,
/// Selector ending with `>> internal:control=enter-frame`
selector: String,
page: Page,
}
impl FrameLocator {
/// Creates a new FrameLocator from a frame selector.
///
/// The `frame_selector` identifies the iframe element (e.g., `"iframe[name='content']"`).
/// The resulting FrameLocator's internal selector appends `>> internal:control=enter-frame`.
pub(crate) fn new(frame: Arc<Frame>, frame_selector: String, page: Page) -> Self {
let selector = format!("{} >> internal:control=enter-frame", frame_selector);
Self {
frame,
selector,
page,
}
}
/// Creates a [`Locator`] for elements inside this iframe.
///
/// The resulting Locator's selector crosses the iframe boundary via the
/// `internal:control=enter-frame` directive.
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-locator>
pub fn locator(&self, selector: &str) -> Locator {
Locator::new(
Arc::clone(&self.frame),
format!("{} >> {}", self.selector, selector),
self.page.clone(),
)
}
/// Creates a nested [`FrameLocator`] for an iframe within this iframe.
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-frame-locator>
pub fn frame_locator(&self, selector: &str) -> FrameLocator {
FrameLocator {
frame: Arc::clone(&self.frame),
selector: format!(
"{} >> {} >> internal:control=enter-frame",
self.selector, selector
),
page: self.page.clone(),
}
}
/// Returns a [`Locator`] for the iframe element itself (the `<iframe>` tag).
///
/// This is the selector *without* the `>> internal:control=enter-frame` suffix.
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-owner>
pub fn owner(&self) -> Locator {
// Strip the trailing " >> internal:control=enter-frame"
let owner_selector = self
.selector
.strip_suffix(" >> internal:control=enter-frame")
.unwrap_or(&self.selector)
.to_string();
Locator::new(Arc::clone(&self.frame), owner_selector, self.page.clone())
}
/// Returns a new FrameLocator matching the first iframe.
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-first>
pub fn first(&self) -> FrameLocator {
self.nth(0)
}
/// Returns a new FrameLocator matching the last iframe.
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-last>
pub fn last(&self) -> FrameLocator {
self.nth(-1)
}
/// Returns a new FrameLocator matching the nth iframe (0-indexed).
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-nth>
pub fn nth(&self, index: i32) -> FrameLocator {
// Insert nth before the enter-frame control:
// "iframe >> internal:control=enter-frame"
// becomes "iframe >> nth=N >> internal:control=enter-frame"
let owner_selector = self
.selector
.strip_suffix(" >> internal:control=enter-frame")
.unwrap_or(&self.selector);
FrameLocator {
frame: Arc::clone(&self.frame),
selector: format!(
"{} >> nth={} >> internal:control=enter-frame",
owner_selector, index
),
page: self.page.clone(),
}
}
// ========================================================================
// get_by_* convenience methods — delegate to self.locator(selector)
// ========================================================================
/// Locate by text content inside the iframe.
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-get-by-text>
pub fn get_by_text(&self, text: &str, exact: bool) -> Locator {
self.locator(&get_by_text_selector(text, exact))
}
/// Locate by associated label text inside the iframe.
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-get-by-label>
pub fn get_by_label(&self, text: &str, exact: bool) -> Locator {
self.locator(&get_by_label_selector(text, exact))
}
/// Locate by placeholder text inside the iframe.
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-get-by-placeholder>
pub fn get_by_placeholder(&self, text: &str, exact: bool) -> Locator {
self.locator(&get_by_placeholder_selector(text, exact))
}
/// Locate by alt text inside the iframe.
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-get-by-alt-text>
pub fn get_by_alt_text(&self, text: &str, exact: bool) -> Locator {
self.locator(&get_by_alt_text_selector(text, exact))
}
/// Locate by title attribute inside the iframe.
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-get-by-title>
pub fn get_by_title(&self, text: &str, exact: bool) -> Locator {
self.locator(&get_by_title_selector(text, exact))
}
/// Locate by `data-testid` attribute inside the iframe.
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-get-by-test-id>
pub fn get_by_test_id(&self, test_id: &str) -> Locator {
self.locator(&get_by_test_id_selector(test_id))
}
/// Locate by ARIA role inside the iframe.
///
/// See: <https://playwright.dev/docs/api/class-framelocator#frame-locator-get-by-role>
pub fn get_by_role(&self, role: AriaRole, options: Option<GetByRoleOptions>) -> Locator {
self.locator(&get_by_role_selector(role, options))
}
}
impl std::fmt::Debug for FrameLocator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FrameLocator")
.field("selector", &self.selector)
.finish()
}
}