-
Notifications
You must be signed in to change notification settings - Fork 1
/
elem_filter.R
339 lines (287 loc) · 8.22 KB
/
elem_filter.R
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#' Extract a subset of HTML elements
#'
#' @description
#' Operators to extract a subset of elements, or a single element, from
#' a selenider element collection.
#'
#' `elem_filter()` and `elem_find()` allow you to use conditions to filter HTML
#' elements (see [is_present()] and other conditions). `elem_find()` returns
#' the *first* element that satisfies one or more conditions, while
#' `elem_filter()` returns every element that satisfies these conditions.
#'
#' `[` and `[[` with a numeric subscript can be used on an element collection
#' to filter the elements by position. `[` returns a single element at a
#' specified location, while `[[` returns a collection of the elements at more
#' than one position.
#'
#' @param x A `selenider_elements` object.
#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> Conditions (functions or
#' function calls) that are used to filter the elements of `x`.
#' @param i A number (or for `[`, a vector of one or more numbers) used to
#' select elements by position.
#'
#' @details
#' As with the [find_element()] and [find_elements()] functions, these
#' functions are lazy, meaning that the elements are not fetched and filtered
#' until they are needed.
#'
#' Conditions can be functions or function calls (see [elem_expect()] for more
#' details).
#'
#' @returns
#' `elem_filter()` and `[` return a `selenider_elements` object, since they can
#' result in multiple elements.
#' `elem_find()` and `[[` return a single `selenider_element` object.
#'
#' @seealso
#' * [find_elements()] and [ss()] to get elements to filter.
#' * [is_present()] and other conditions for predicates for HTML elements.
#' (If you scroll down to the *See also* section, you will find the rest).
#'
#' @examplesIf selenider::selenider_available(online = FALSE)
#' html <- "
#' <button disabled>Button 1</button>
#' <button>Button 2</button>
#' <p>Text</p>
#' <div style='display:none;'></div>
#' "
#' session <- minimal_selenider_session(html)
#'
#' elements <- ss("*")
#'
#' # Gives the same result as s()
#' elements[[1]]
#'
#' elements[1:3]
#'
#' elements[-2]
#'
#' elements |>
#' elem_filter(is_visible)
#'
#' elements |>
#' elem_find(is_visible)
#'
#' # The above is equivalent to:
#' visible_elems <- elements |>
#' elem_filter(is_visible)
#' visible_elems[[1]]
#'
#' # In R >= 4.3.0, we can instead do:
#' # ss(".class1") |>
#' # elem_filter(is_visible) |>
#' # _[[1]]
#'
#' ss("button") |>
#' elem_filter(is_enabled)
#'
#' @export
elem_filter <- function(x, ...) {
check_class(x, "selenider_elements")
if (elements_is_empty(x)) {
return(x)
}
exprs <- enquos(...)
arg_names <- lapply(
c("a1", "a2", "a3", "a4"),
function(x) make_elem_name(exprs, x)
)
elem_name <- make_elem_name(exprs)
elem_expr <- filter_elem_name(elem_name, arg_names)
calls <- lapply(exprs, parse_condition, elem_expr)
functions <- mapply(
condition_to_function,
calls,
exprs,
MoreArgs = list(
elem_name = elem_name,
arg_names = arg_names,
session = x$session,
driver = x$driver,
driver_id = x$driver_id,
timeout = x$timeout
),
SIMPLIFY = FALSE
)
selectors <- x$selectors
x$selectors[[length(selectors)]]$filter <-
c(x$selectors[[length(selectors)]]$filter, functions)
x$selectors[[length(x$selectors)]]$to_be_filtered <-
x$selectors[[length(x$selectors)]]$to_be_filtered + 1
x
}
#' @rdname elem_filter
#'
#' @export
elem_find <- function(x, ...) {
if (elements_is_empty(x)) {
stop_find_empty_elements()
}
x <- elem_filter(x, ...)
x <- add_numeric_filter(x, 1, max_subscript_error = TRUE, multiple = FALSE)
class(x) <- "selenider_element"
x
}
#' @rdname elem_filter
#'
#' @export
`[.selenider_elements` <- function(x, i) {
i <- vctrs::vec_cast(i, integer())
i <- stats::na.omit(i)
i <- i[i != 0]
if (elements_is_empty(x)) {
if (all(i > 0L)) {
warn_subscript_max_length(max(i), 0)
}
return(x)
}
if (length(i) == 0) {
return(empty_selenider_elements(x$driver, x$driver_id))
}
check_subscript_vctrs(i)
add_numeric_filter(x, i)
}
# Checks that `i` is invalid using vctrs
# If `i` can be used to subset a vector, then it can be used to subset
# a selenider element.
check_subscript_vctrs <- function(i, call = rlang::caller_env()) {
len <- max(abs(i))
x <- rep.int(1L, len)
# Check that i is a valid subscript
vctrs::vec_slice(x, i, error_call = call)
}
#' @rdname elem_filter
#'
#' @export
`[[.selenider_elements` <- function(x, i) {
if (!is.vector(i)) {
stop_subscript_type(i)
} else if (isTRUE(is.na(i))) {
stop_subscript_na(i)
} else if (!is.numeric(i)) {
NextMethod()
} else if (length(i) != 1) {
stop_subscript_length(i)
}
check_number_whole(i)
i <- vctrs::vec_cast(i, integer())
if (i == 0) {
stop_subscript_0()
}
if (elements_is_empty(x)) {
stop_subscript_max_length(i, 0L)
}
x <- add_numeric_filter(x, i, max_subscript_error = TRUE, multiple = FALSE)
class(x) <- "selenider_element"
x
}
add_numeric_filter <- function(x,
i,
call = rlang::caller_env(),
max_subscript_error = FALSE,
multiple = TRUE) {
selectors <- x$selectors
filters <- selectors[[length(selectors)]]$filter
is_numeric_and_positive <- function(x) {
is.numeric(x) && length(x) > 0 && all(x > 0)
}
numeric_filters <- Filter(is_numeric_and_positive, filters)
min_length <- if (length(numeric_filters) != 0) {
min(lengths(numeric_filters))
} else {
Inf
}
max_sub <- max(i, na.rm = TRUE)
if (max_sub >= min_length) {
if (max_subscript_error) {
stop_subscript_max_length(max_sub, min_length, call = call)
} else {
warn_subscript_max_length(max_sub, min_length, call = call)
}
}
if (length(filters) != 0 && is.numeric(filters[[length(filters)]])) {
filter <- filters[[length(filters)]]
if (all(filter > 0)) {
new_filter <- filter[i]
} else if (all(i > 0)) {
new_filter <- seq_len(max(i, na.rm = TRUE) + length(filter))[filter][i]
} else {
new_filter <- unique(c(filter, i))
}
new_filter <- stats::na.omit(new_filter)
if (length(new_filter) == 0) {
return(empty_selenider_elements(x$driver, x$driver_id))
}
x$selectors[[length(selectors)]]$filter[[length(filters)]] <- new_filter
} else {
x$selectors[[length(selectors)]]$filter <-
append(x$selectors[[length(selectors)]]$filter, list(i))
}
x$selectors[[length(x$selectors)]]$to_be_filtered <-
x$selectors[[length(x$selectors)]]$to_be_filtered + 1
x$selectors[[length(x$selectors)]]$multiple <- multiple
x
}
condition_to_function <- function(x,
original_call,
elem_name,
arg_names,
session,
driver,
driver_id,
timeout) {
extra_data <- list(
session,
driver,
driver_id,
timeout,
webelement_to_element = webelement_to_element,
webelements_to_elements = webelements_to_elements
)
names(extra_data)[1:4] <- arg_names
args <- pairlist2(x = )
names(args) <- elem_name
res <- new_function(
args,
call2(with_timeout, 0, quo_get_expr(x)),
new_environment(extra_data, parent = quo_get_env(x))
)
attr(res, "original_call") <- quo_get_expr(original_call)
res
}
filter_elem_name <- function(elem_name, arg_names) {
args <- paste(
elem_name,
arg_names[[1]],
arg_names[[2]],
arg_names[[3]],
arg_names[[4]],
sep = ", "
)
paste0("webelement_to_element(", args, ")")
}
webelement_to_element <- function(x, session, driver, driver_id, timeout) {
res <- list(
session = session,
driver = driver,
driver_id = driver_id,
element = x,
timeout = timeout,
selectors = list(),
to_be_found = 0
)
class(res) <- "selenider_element"
res
}
webelements_to_elements <- function(x, driver, driver_id, timeout) {
res <- list(
driver = driver,
driver_id = driver_id,
element = x,
timeout = timeout,
selectors = list(),
to_be_found = 0
)
class(res) <- c("selenider_elements", "list")
res
}