WebElement.pm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. package Selenium::Remote::WebElement;
  2. # ABSTRACT: Representation of an HTML Element used by Selenium Remote Driver
  3. use strict;
  4. use warnings;
  5. use Moo;
  6. use Carp qw(carp croak);
  7. =head1 DESCRIPTION
  8. Selenium Webdriver represents all the HTML elements as WebElements.
  9. This module provides a mechanism to represent them as objects &
  10. perform various actions on the related elements. This module should
  11. not be instantiated directly by the end user. Selenium::Remote::Driver
  12. instantiates this module when required. Typically, the find_element
  13. method in Selenium::Remote::Driver returns this object on which
  14. various element related operations can be carried out.
  15. What is probably most useful on this page is the list of methods below
  16. that you can perform on an element once you've found one and S::R::D
  17. has made an instance of this for you.
  18. =head1 CONSTRUCTOR
  19. =head2 new
  20. =over 4
  21. =item B<id>
  22. Required: Pass in a string representing the ID of the object. The
  23. string should be obtained from the response object of making one of
  24. the C<find_element> calls from L<Selenium::Remote::Driver>.
  25. The attribute is also set up to handle spec compliant element response
  26. objects via its `coerce` such that any of the following will work and
  27. are all equivalent:
  28. my $old_elem = Selenium::Remote::WebElement->new(
  29. id => 1,
  30. driver => $driver
  31. );
  32. my $new_remote_elem = Selenium::Remote::WebElement->new(
  33. id => { ELEMENT => 1 },
  34. driver => $driver
  35. );
  36. my $new_spec_elem = Selenium::Remote::WebElement->new(
  37. id => { 'element-6066-11e4-a52e-4f735466cecf' => 1 },
  38. driver => $driver
  39. );
  40. and then after instantiation, all three would give the following for
  41. `id`:
  42. print $elem->id; # prints 1
  43. =item B<driver>
  44. Required: Pass in a Selenium::Remote::Driver instance or one of its
  45. subclasses. The WebElement needs the appropriate Driver session to
  46. execute its commands properly.
  47. =back
  48. For typical usage of S::R::D and this module, none of this
  49. matters and it should Just Work without you having to worry about it
  50. at all. For further reading, the L<W3C
  51. spec|https://www.w3.org/TR/webdriver/#elements> strictly dictates the
  52. exact behavior.
  53. =cut
  54. has 'id' => (
  55. is => 'ro',
  56. required => 1,
  57. coerce => sub {
  58. my ($value) = @_;
  59. if ( ref($value) eq 'HASH' ) {
  60. if ( exists $value->{ELEMENT} ) {
  61. # The JSONWireProtocol web element object looks like
  62. #
  63. # { "ELEMENT": $INTEGER_ID }
  64. return $value->{ELEMENT};
  65. }
  66. elsif ( exists $value->{'element-6066-11e4-a52e-4f735466cecf'} ) {
  67. # but the WebDriver spec web element uses a magic
  68. # string. See the spec for more information:
  69. #
  70. # https://www.w3.org/TR/webdriver/#elements
  71. return $value->{'element-6066-11e4-a52e-4f735466cecf'};
  72. }
  73. else {
  74. croak
  75. 'When passing in an object to the WebElement id attribute, it must have at least one of the ELEMENT or element-6066-11e4-a52e-4f735466cecf keys.';
  76. }
  77. }
  78. else {
  79. return $value;
  80. }
  81. }
  82. );
  83. has 'driver' => (
  84. is => 'ro',
  85. required => 1,
  86. handles => [qw(_execute_command)],
  87. );
  88. =head1 FUNCTIONS
  89. =head2 child(selector, method)
  90. =head2 children(selector, method)
  91. Alias to Selenium::Remote::Driver::find_child_element and find_child_elements, respectively.
  92. =cut
  93. sub child {
  94. return $_[0]->{driver}->find_child_element(@_);
  95. }
  96. sub children {
  97. return $_[0]->{driver}->find_child_elements(@_);
  98. }
  99. =head2 click
  100. Description:
  101. Click the element.
  102. Usage:
  103. $elem->click();
  104. =cut
  105. sub click {
  106. my ($self) = @_;
  107. my $res = { 'command' => 'clickElement', 'id' => $self->id };
  108. return $self->_execute_command($res);
  109. }
  110. =head2 execute_script($script, @args), execute_async_script($script, @args)
  111. Convenience method to execute a script with the element passed as the first argument to the script function, and the remaining args appended.
  112. See the documentation for Selenium::Remote::Driver::execute_script for more information.
  113. =cut
  114. sub execute_script {
  115. my ($self, $script, @args) = @_;
  116. return $self->driver->execute_script(
  117. $script,
  118. { 'element-6066-11e4-a52e-4f735466cecf' => $self->{id} },
  119. @args );
  120. }
  121. sub execute_async_script {
  122. my ($self, $script, @args) = @_;
  123. return $self->driver->execute_async_script(
  124. $script,
  125. { 'element-6066-11e4-a52e-4f735466cecf' => $self->{id} },
  126. @args );
  127. }
  128. =head2 submit
  129. Description:
  130. Submit a FORM element. The submit command may also be applied to any element
  131. that is a descendant of a FORM element.
  132. Compatibility:
  133. On webdriver3 enabled servers, this uses a JS shim, which WILL NOT submit correctly unless your element is an <input>.
  134. Try clicking it if possible instead.
  135. Usage:
  136. $elem->submit();
  137. =cut
  138. sub submit {
  139. my ($self) = @_;
  140. if (
  141. $self->driver->{is_wd3}
  142. && !(
  143. grep { $self->driver->browser_name eq $_ } qw{MicrosoftEdge}
  144. )
  145. )
  146. {
  147. if ( $self->get_tag_name() ne 'form' ) {
  148. return $self->driver->execute_script(
  149. "return arguments[0].form.submit();",
  150. { 'element-6066-11e4-a52e-4f735466cecf' => $self->{id} } );
  151. }
  152. else {
  153. return $self->driver->execute_script(
  154. "return arguments[0].submit();",
  155. { 'element-6066-11e4-a52e-4f735466cecf' => $self->{id} } );
  156. }
  157. }
  158. my $res = { 'command' => 'submitElement', 'id' => $self->id };
  159. return $self->_execute_command($res);
  160. }
  161. =head2 send_keys
  162. Description:
  163. Send a sequence of key strokes to an element. If you want to send specific
  164. Keyboard events, then use the WDKeys module along with theis method. See e.g.
  165. for reference
  166. Input: 1
  167. Required:
  168. {ARRAY | STRING} - Array of strings or a string.
  169. Usage:
  170. $elem->send_keys('abcd', 'efg');
  171. $elem->send_keys('hijk');
  172. or
  173. # include the WDKeys module
  174. use Selenium::Remote::WDKeys;
  175. .
  176. .
  177. $elem->send_keys(KEYS->{'space'}, KEYS->{'enter'});
  178. =cut
  179. sub send_keys {
  180. my ( $self, @strings ) = @_;
  181. croak "no keys to send" unless scalar @strings >= 1;
  182. my $res = { 'command' => 'sendKeysToElement', 'id' => $self->id };
  183. # We need to send an array of single characters to be WebDriver
  184. # spec compatible. That is, for @strings = ('hel', 'lo'), the
  185. # corresponding value must be ('h', 'e', 'l', 'l', 'o' ). This
  186. # format conforms with the Spec AND works with the Selenium
  187. # standalone server.
  188. my $strings = join( '', map { $_ . "" } @strings );
  189. my $params = {
  190. 'value' => [ split( '', $strings ) ],
  191. text => $strings,
  192. };
  193. return $self->_execute_command( $res, $params );
  194. }
  195. =head2 is_selected
  196. Description:
  197. Determine if an OPTION element, or an INPUT element of type checkbox or
  198. radiobutton is currently selected.
  199. Output:
  200. BOOLEAN - whether the element is selected
  201. Usage:
  202. $elem->is_selected();
  203. =cut
  204. sub is_selected {
  205. my ($self) = @_;
  206. my $to_check = $self->get_tag_name() eq 'option' ? 'selected' : 'checked';
  207. return $self->get_property($to_check)
  208. if $self->driver->{is_wd3}
  209. && !( grep { $self->driver->browser_name eq $_ }
  210. qw{chrome MicrosoftEdge} );
  211. my $res = { 'command' => 'isElementSelected', 'id' => $self->id };
  212. return $self->_execute_command($res);
  213. }
  214. =head2 set_selected
  215. Description:
  216. Select an OPTION element, or an INPUT element of type checkbox or radiobutton.
  217. Forces selected=1 on the element..
  218. Usage:
  219. $elem->set_selected();
  220. =cut
  221. sub set_selected {
  222. my ($self) = @_;
  223. if ( $self->driver->{is_wd3} ) {
  224. return if $self->is_selected();
  225. return $self->click();
  226. }
  227. my $res = { 'command' => 'setElementSelected', 'id' => $self->id };
  228. return $self->_execute_command($res);
  229. }
  230. =head2 toggle
  231. Description:
  232. Toggle whether an OPTION element, or an INPUT element of type checkbox or
  233. radiobutton is currently selected.
  234. Output:
  235. BOOLEAN - Whether the element is selected after toggling its state.
  236. Usage:
  237. $elem->toggle();
  238. =cut
  239. sub toggle {
  240. my ($self) = @_;
  241. if ( $self->driver->{is_wd3} ) {
  242. return $self->click() unless $self->is_selected();
  243. return $self->driver->execute_script(
  244. qq/ if (arguments[0].checked) { arguments[0].checked = 0 }; return arguments[0].checked; /,
  245. { 'element-6066-11e4-a52e-4f735466cecf' => $self->{id} }
  246. );
  247. }
  248. my $res = { 'command' => 'toggleElement', 'id' => $self->id };
  249. return $self->_execute_command($res);
  250. }
  251. =head2 is_enabled
  252. Description:
  253. Determine if an element is currently enabled.
  254. Output:
  255. BOOLEAN - Whether the element is enabled.
  256. Usage:
  257. $elem->is_enabled();
  258. =cut
  259. sub is_enabled {
  260. my ($self) = @_;
  261. if (
  262. $self->driver->{is_wd3}
  263. && !(
  264. grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge}
  265. )
  266. )
  267. {
  268. return 1 if $self->get_tag_name() ne 'input';
  269. return $self->get_property('disabled') ? 0 : 1;
  270. }
  271. my $res = { 'command' => 'isElementEnabled', 'id' => $self->id };
  272. return $self->_execute_command($res);
  273. }
  274. =head2 get_element_location
  275. Description:
  276. Determine an element's location on the page. The point (0, 0) refers to the
  277. upper-left corner of the page.
  278. Compatibility:
  279. On WebDriver 3 enabled servers, this is an alias for get_element_rect().
  280. Output:
  281. HASH - The X and Y coordinates for the element on the page.
  282. Usage:
  283. $elem->get_element_location();
  284. This method is DEPRECATED on webdriver3 enabled servers.
  285. =cut
  286. sub get_element_location {
  287. my ($self) = @_;
  288. if (
  289. $self->driver->{is_wd3}
  290. && !(
  291. grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge}
  292. )
  293. )
  294. {
  295. my $data = $self->get_element_rect();
  296. delete $data->{height};
  297. delete $data->{width};
  298. return $data;
  299. }
  300. my $res = { 'command' => 'getElementLocation', 'id' => $self->id };
  301. return $self->_execute_command($res);
  302. }
  303. =head2 get_size
  304. Description:
  305. Determine an element's size in pixels. The size will be returned with width
  306. and height properties.
  307. Compatibility:
  308. On WebDriver 3 enabled servers, this is an alias for get_element_rect().
  309. Output:
  310. HASH - The width and height of the element, in pixels.
  311. Usage:
  312. $elem->get_size();
  313. This method is DEPRECATED on webdriver3 enabled servers.
  314. =cut
  315. sub get_size {
  316. my ($self) = @_;
  317. if (
  318. $self->driver->{is_wd3}
  319. && !(
  320. grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge}
  321. )
  322. )
  323. {
  324. my $data = $self->get_element_rect();
  325. delete $data->{x};
  326. delete $data->{y};
  327. return $data;
  328. }
  329. my $res = { 'command' => 'getElementSize', 'id' => $self->id };
  330. return $self->_execute_command($res);
  331. }
  332. =head2 get_element_rect
  333. Get the element's size AND location in a hash.
  334. Example Output:
  335. { x => 0, y => 0, height => 10, width => 10 }
  336. =cut
  337. sub get_element_rect {
  338. my ($self) = @_;
  339. my $res = { 'command' => 'getElementRect', 'id' => $self->id };
  340. return $self->_execute_command($res);
  341. }
  342. =head2 get_element_location_in_view
  343. Description:
  344. Determine an element's location on the screen once it has been scrolled
  345. into view.
  346. Note: This is considered an internal command and should only be used to
  347. determine an element's location for correctly generating native events.
  348. Compatibility:
  349. On Webdriver3 servers, we have to implement this with a JS shim.
  350. This means in some contexts, you won't get any position returned, as the element isn't considered an element internally.
  351. You may have to go up the element stack to find the element that actually has the bounding box.
  352. Output:
  353. {x:number, y:number} The X and Y coordinates for the element on the page.
  354. Usage:
  355. $elem->get_element_location_in_view();
  356. =cut
  357. sub get_element_location_in_view {
  358. my ($self) = @_;
  359. #XXX chrome is dopey here
  360. return $self->driver->execute_script(
  361. qq{
  362. if (typeof(arguments[0]) !== 'undefined' && arguments[0].nodeType === Node.ELEMENT_NODE) {
  363. arguments[0].scrollIntoView();
  364. var pos = arguments[0].getBoundingClientRect();
  365. return {y:pos.top,x:pos.left};
  366. }
  367. return {};
  368. }, { 'element-6066-11e4-a52e-4f735466cecf' => $self->{id} }
  369. )
  370. if $self->driver->{is_wd3} && grep { $self->driver->browser_name eq $_ }
  371. ( 'firefox', 'internet explorer', 'chrome' );
  372. my $res = { 'command' => 'getElementLocationInView', 'id' => $self->id };
  373. return $self->_execute_command($res);
  374. }
  375. =head2 get_tag_name
  376. Description:
  377. Query for an element's tag name.
  378. Output:
  379. STRING - The element's tag name, as a lowercase string.
  380. Usage:
  381. $elem->get_tag_name();
  382. =cut
  383. sub get_tag_name {
  384. my ($self) = @_;
  385. my $res = { 'command' => 'getElementTagName', 'id' => $self->id };
  386. return $self->_execute_command($res);
  387. }
  388. =head2 clear
  389. Description:
  390. Clear a TEXTAREA or text INPUT element's value.
  391. Usage:
  392. $elem->clear();
  393. =cut
  394. sub clear {
  395. my ($self) = @_;
  396. my $res = { 'command' => 'clearElement', 'id' => $self->id };
  397. return $self->_execute_command($res);
  398. }
  399. =head2 get_attribute
  400. Description:
  401. Get the value of an element's attribute.
  402. Compatibility:
  403. In older webDriver, this actually got the value of an element's property.
  404. If you want to get the initial condition (e.g. the values in the tag hardcoded in HTML), pass 1 as the second argument.
  405. Or, set $driver->{emulate_jsonwire} = 0 to not have to pass the extra arg.
  406. This can only done on WebDriver 3 enabled servers.
  407. Input: 2
  408. Required:
  409. STRING - name of the attribute of the element
  410. Optional:
  411. BOOLEAN - "I really mean that I want the initial condition, quit being so compatible!!!"
  412. Output:
  413. {STRING | NULL} The value of the attribute, or null if it is not set on the element.
  414. Usage:
  415. $elem->get_attribute('name',1);
  416. =cut
  417. sub get_attribute {
  418. my ( $self, $attr_name, $no_i_really_mean_it ) = @_;
  419. if ( not defined $attr_name ) {
  420. croak 'Attribute name not provided';
  421. }
  422. #Handle global JSONWire emulation flag
  423. $no_i_really_mean_it = 1 unless $self->{driver}->{emulate_jsonwire};
  424. return $self->get_property($attr_name)
  425. if $self->driver->{is_wd3}
  426. && !( grep { $self->driver->browser_name eq $_ }
  427. qw{chrome MicrosoftEdge} )
  428. && !$no_i_really_mean_it;
  429. my $res = {
  430. 'command' => 'getElementAttribute',
  431. 'id' => $self->id,
  432. 'name' => $attr_name,
  433. };
  434. return $self->_execute_command($res);
  435. }
  436. =head2 get_property
  437. Gets the C<Current Value> of an element's attribute.
  438. Takes a named property as an argument.
  439. Only available on WebDriver 3 enabled servers.
  440. =cut
  441. sub get_property {
  442. my ( $self, $prop ) = @_;
  443. return $self->get_attribute($prop)
  444. if $self->driver->{is_wd3}
  445. && ( grep { $self->driver->browser_name eq $_ }
  446. qw{chrome MicrosoftEdge} );
  447. my $res =
  448. { 'command' => 'getElementProperty', id => $self->id, name => $prop };
  449. return $self->_execute_command($res);
  450. }
  451. =head2 get_value
  452. Description:
  453. Query for the value of an element, as determined by its value attribute.
  454. Output:
  455. {STRING | NULL} The element's value, or null if it doesn't have a value attribute.
  456. Usage:
  457. $elem->get_value();
  458. =cut
  459. sub get_value {
  460. my ($self) = @_;
  461. return $self->get_attribute('value');
  462. }
  463. =head2 is_displayed
  464. Description:
  465. Determine if an element is currently displayed.
  466. Note: This does *not* tell you an element's 'visibility' property; as it still takes up space in the DOM and is therefore considered 'displayed'.
  467. WC3 Compatibility:
  468. On JSONWire this method really only checked to see whether the element's style was display:none, or whether it was a hidden input.
  469. This is because "displayedness" was pretty loosely defined until fairly late on into the process, and much grief resulted.
  470. In WC3 webdriver, it additionally does a viewport check, to account for the firmer definition of "displayedness":
  471. https://w3c.github.io/webdriver/#element-displayedness
  472. Output:
  473. BOOLEAN - Whether the element is displayed.
  474. Usage:
  475. $elem->is_displayed();
  476. =cut
  477. sub is_displayed {
  478. my ($self) = @_;
  479. if (
  480. $self->driver->{is_wd3}
  481. && !(
  482. grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge}
  483. )
  484. )
  485. {
  486. return 0
  487. if $self->get_tag_name() eq 'input'
  488. && $self->get_property('type') eq 'hidden'; #hidden type inputs
  489. return 0 unless $self->_is_in_viewport();
  490. return int( $self->get_css_attribute('display') ne 'none' );
  491. }
  492. my $res = { 'command' => 'isElementDisplayed', 'id' => $self->id };
  493. return $self->_execute_command($res);
  494. }
  495. sub _is_in_viewport {
  496. my ($self) = @_;
  497. return $self->driver->execute_script(
  498. qq{
  499. var rect = arguments[0].getBoundingClientRect();
  500. return (
  501. rect.top >= 0 &&
  502. rect.left >= 0 &&
  503. rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
  504. rect.right <= (window.innerWidth || document.documentElement.clientWidth)
  505. );
  506. }, { 'element-6066-11e4-a52e-4f735466cecf' => $self->{id} }
  507. );
  508. }
  509. =head2 is_hidden
  510. Description:
  511. Determine if an element is currently hidden.
  512. Output:
  513. BOOLEAN - Whether the element is hidden.
  514. Usage:
  515. $elem->is_hidden();
  516. =cut
  517. sub is_hidden {
  518. my ($self) = @_;
  519. return !$self->is_displayed();
  520. }
  521. =head2 drag
  522. Alias for Selenium::ActionChains::drag_and_drop().
  523. Provide element you wish to drag to as argument.
  524. my $target = $driver->find_element('receptacle','id');
  525. my $subject = $driver->find_element('thingy','id');
  526. $subject->drag($target);
  527. =cut
  528. sub drag {
  529. my ( $self, $target ) = @_;
  530. require Selenium::ActionChains;
  531. my $chain = Selenium::ActionChains->new( driver => $self->driver );
  532. return $chain->drag_and_drop( $self, $target )->perform();
  533. }
  534. =head2 get_text
  535. Description:
  536. Get the innerText of the element.
  537. Output:
  538. STRING - innerText of an element
  539. Usage:
  540. $elem->get_text();
  541. =cut
  542. sub get_text {
  543. my ($self) = @_;
  544. my $res = { 'command' => 'getElementText', 'id' => $self->id };
  545. return $self->_execute_command($res);
  546. }
  547. =head2 get_css_attribute
  548. Description:
  549. Query the value of an element's computed CSS property. The CSS property to
  550. query should be specified using the CSS property name, not the JavaScript
  551. property name (e.g. background-color instead of backgroundColor).
  552. Input: 1
  553. Required:
  554. STRING - name of the css-attribute
  555. Output:
  556. STRING - Value of the css attribute
  557. Usage:
  558. $elem->get_css_attribute('background-color');
  559. =cut
  560. sub get_css_attribute {
  561. my ( $self, $attr_name ) = @_;
  562. if ( not defined $attr_name ) {
  563. croak 'CSS attribute name not provided';
  564. }
  565. my $res = {
  566. 'command' => 'getElementValueOfCssProperty',
  567. 'id' => $self->id,
  568. 'property_name' => $attr_name,
  569. };
  570. return $self->_execute_command($res);
  571. }
  572. =head2 describe
  573. Description:
  574. Describe the identified element
  575. Usage:
  576. $elem->describe();
  577. Note: DEPRECATED as of 2.42.2 -- use get_text, get_value, is_displayed, or
  578. whatever appropriate WebElement function you need instead
  579. Entirely unsupported on WebDriver 3 enabled servers.
  580. =cut
  581. sub describe {
  582. my ($self) = @_;
  583. my $res = { 'command' => 'describeElement', 'id' => $self->id };
  584. return $self->_execute_command($res);
  585. }
  586. =head2 screenshot
  587. Description:
  588. Get a screenshot of the visible region that is a subset of the element's bounding box as a base64 encoded image.
  589. Compatibility:
  590. Only available on Webdriver3 enabled selenium servers.
  591. Input (optional):
  592. $scroll_into_view - BOOLEAN default true. If false, will not scroll the element into the viewport first.
  593. Failing to do so may result in an image being cropped partially or entirely.
  594. Output:
  595. STRING - base64 encoded image
  596. Usage:
  597. print $element->screenshot();
  598. To conveniently write the screenshot to a file, see L</capture_screenshot>.
  599. =cut
  600. sub screenshot {
  601. my ( $self, $scroll ) = @_;
  602. $scroll //= 1;
  603. my $res = { 'command' => 'elementScreenshot', id => $self->id };
  604. my $input = { scroll => int($scroll) };
  605. return $self->_execute_command( $res, $input );
  606. }
  607. =head2 capture_screenshot
  608. Description:
  609. Capture a screenshot of said element and save as a PNG to provided file name.
  610. Compatibility:
  611. Only available on Webdriver3 enabled selenium servers.
  612. Input (optional):
  613. $scroll_into_view - BOOLEAN default true. If false, will not scroll the element into the viewport first.
  614. Failing to do so may result in an image being cropped partially or entirely.
  615. Output:
  616. TRUE - (Screenshot is written to file)
  617. Usage:
  618. $element->capture_screenshot($filename);
  619. =cut
  620. sub capture_screenshot {
  621. my ( $self, $filename, $scroll ) = @_;
  622. croak '$filename is required' unless $filename;
  623. open( my $fh, '>', $filename );
  624. binmode $fh;
  625. print $fh MIME::Base64::decode_base64( $self->screenshot($scroll) );
  626. CORE::close $fh;
  627. return 1;
  628. }
  629. 1;