Nakilon
Nakilon

Reputation: 35102

How do I find Capybara::Node::Element that corresponds to Selenium::WebDriver::Element

There is a node, that I find by coordinates and wish to execute a relative xpath query on it, so I probably need a Capybara object (Capybara::Node::Element) for .find(:xpath, but the object I get from evaluate_script("document.elementFromPoint is a Selenium object (Selenium::WebDriver::Element).

Upvotes: 1

Views: 687

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49950

Capybara::Node::Element is just a wrapper around the native node (that also stores how it was found so it can be auto reloaded). Snce you won't be using the auto reload behavior you can probably just do

Capybara::Node::Element.new(current_session, element_from_evaluate, nil, nil)

Upvotes: 1

Mesut GUNES
Mesut GUNES

Reputation: 7421

you can use .attribute("class") for getting class of element then produce the css_selector and finally find(:css, css_selector) returns Capybara::Node::Element.

irb#1(main):139:0* p = evaluate_script("document.elementFromPoint(1000, 240)")
=> #<Selenium::WebDriver::Element:0x5cc6ed77c104c804 id="{0b83a9a2-ff2c-fe43-8518-38b7522c47a9}">
irb#1(main):140:0> c = p.attribute("class")
=> "col-xs-12 col-sm-4"
irb#1(main):141:0> cc = c.split(" ")
=> ["col-xs-12", "col-sm-4"]
irb#1(main):142:0> css = ""
=> ""
irb#1(main):143:0> cc.each { |cs| css << (".#{cs}")}
=> ["col-xs-12", "col-sm-4"]
irb#1(main):144:0> css
=> ".col-xs-12.col-sm-4"
irb#1(main):145:0> find(:css, css, :match => :first)
=> #<Capybara::Node::Element tag="div" path="/html/body/div[4]/div[1]/div[2]">

I think you can also use :css instead of :xpath

Upvotes: 0

Related Questions