Class: Puppeteer::Bidi::NodeLocator

Inherits:
Locator
  • Object
show all
Defined in:
lib/puppeteer/bidi/locator.rb,
sig/puppeteer/bidi/locator.rbs

Overview

Locator that queries nodes by selector or handle.

Constant Summary

Constants inherited from Locator

Locator::RETRY_DELAY

Instance Attribute Summary

Attributes inherited from Locator

#ensure_element_is_in_viewport, #timeout, #visibility, #wait_for_enabled, #wait_for_stable_bounding_box

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Locator

#build_deadline, #click, #clone, #copy_options, #ensure_element_is_in_viewport?, #ensure_element_is_in_viewport_if_needed, #fill, #fill_directly, #filter, #hover, #map, #map_handle, #off, #on, #once, #perform_action, race, #raise_timeout, #remaining_time_ms, #scroll, #set_ensure_element_is_in_the_viewport, #set_timeout, #set_visibility, #set_wait_for_enabled, #set_wait_for_stable_bounding_box, #wait, #wait_for_enabled?, #wait_for_enabled_if_needed, #wait_for_stable_bounding_box?, #wait_for_stable_bounding_box_if_needed, #wait_handle, #wait_until, #with_retry

Constructor Details

#initialize(page_or_frame, selector_or_handle) ⇒ NodeLocator

Returns a new instance of NodeLocator.

Parameters:

  • page_or_frame (Object)
  • selector_or_handle (Object)


682
683
684
685
686
# File 'lib/puppeteer/bidi/locator.rb', line 682

def initialize(page_or_frame, selector_or_handle)
  super()
  @page_or_frame = page_or_frame
  @selector_or_handle = selector_or_handle
end

Class Method Details

.create(page_or_frame, selector) ⇒ Locator

Parameters:

  • page_or_frame (Page, Frame)
  • selector (String)

Returns:



661
662
663
664
665
666
667
668
# File 'lib/puppeteer/bidi/locator.rb', line 661

def self.create(page_or_frame, selector)
  timeout = if page_or_frame.respond_to?(:default_timeout)
              page_or_frame.default_timeout
            else
              page_or_frame.page.default_timeout
            end
  new(page_or_frame, selector).set_timeout(timeout)
end

.create_from_handle(page_or_frame, handle) ⇒ Locator

Parameters:

Returns:



673
674
675
676
677
678
679
680
# File 'lib/puppeteer/bidi/locator.rb', line 673

def self.create_from_handle(page_or_frame, handle)
  timeout = if page_or_frame.respond_to?(:default_timeout)
              page_or_frame.default_timeout
            else
              page_or_frame.page.default_timeout
            end
  new(page_or_frame, handle).set_timeout(timeout)
end

Instance Method Details

#_cloneObject

Returns:

  • (Object)


690
691
692
# File 'lib/puppeteer/bidi/locator.rb', line 690

def _clone
  NodeLocator.new(@page_or_frame, @selector_or_handle).copy_options(self)
end

#_wait(timeout_ms:, deadline:) ⇒ Object

Parameters:

  • timeout_ms: (Object)
  • deadline: (Object)

Returns:

  • (Object)

Raises:



694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# File 'lib/puppeteer/bidi/locator.rb', line 694

def _wait(timeout_ms:, deadline:)
  handle = if @selector_or_handle.is_a?(String)
             query_selector_with_pseudo_selectors(@selector_or_handle)
           else
             @selector_or_handle
           end

  raise RetryableError unless handle
  raise RetryableError if handle.respond_to?(:disposed?) && handle.disposed?

  if visibility
    matches_visibility = case visibility
                         when VisibilityOption::VISIBLE
                           handle.visible?
                         when VisibilityOption::HIDDEN
                           handle.hidden?
                         else
                           true
                         end
    unless matches_visibility
      handle.dispose if @selector_or_handle.is_a?(String) && handle.respond_to?(:dispose)
      raise RetryableError
    end
  end

  handle
end

#p_selector_candidates(selector) ⇒ Object

Parameters:

  • selector (Object)

Returns:

  • (Object)


731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# File 'lib/puppeteer/bidi/locator.rb', line 731

def p_selector_candidates(selector)
  return [selector] unless selector.include?("::-p-")

  parts = split_selector_list(selector)
  candidates = parts.filter_map do |part|
    part = part.strip
    match = part.match(/\A::\-p\-(text|xpath)\((.*)\)\z/)
    next unless match

    name = match[1]
    value = unquote_selector_value(match[2])
    prefix = name == "text" ? "text/" : "xpath/"
    "#{prefix}#{value}"
  end

  candidates.empty? ? [selector] : candidates
end

#query_selector_with_pseudo_selectors(selector) ⇒ Object

Parameters:

  • selector (Object)

Returns:

  • (Object)


722
723
724
725
726
727
728
729
# File 'lib/puppeteer/bidi/locator.rb', line 722

def query_selector_with_pseudo_selectors(selector)
  candidates = p_selector_candidates(selector)
  candidates.each do |candidate|
    handle = @page_or_frame.query_selector(candidate)
    return handle if handle
  end
  nil
end

#split_selector_list(selector) ⇒ Object

Parameters:

  • selector (Object)

Returns:

  • (Object)


749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
# File 'lib/puppeteer/bidi/locator.rb', line 749

def split_selector_list(selector)
  parts = []
  current = +""
  depth = 0
  in_string = nil
  escape = false

  selector.each_char do |char|
    if escape
      current << char
      escape = false
      next
    end

    if in_string
      if char == "\\"
        escape = true
      elsif char == in_string
        in_string = nil
      end
      current << char
      next
    end

    case char
    when "'", '"'
      in_string = char
    when '('
      depth += 1
    when ')'
      depth -= 1 if depth.positive?
    when ','
      if depth.zero?
        parts << current
        current = +""
        next
      end
    end

    current << char
  end

  parts << current unless current.empty?
  parts
end

#unquote_selector_value(value) ⇒ Object

Parameters:

  • value (Object)

Returns:

  • (Object)


795
796
797
798
799
800
801
802
803
804
# File 'lib/puppeteer/bidi/locator.rb', line 795

def unquote_selector_value(value)
  stripped = value.strip
  return stripped if stripped.length < 2

  quote = stripped[0]
  return stripped unless quote == "'" || quote == '"'
  return stripped unless stripped.end_with?(quote)

  stripped[1..-2].gsub(/\\([\s\S])/, '\1')
end