Opening a new tab in Capybara / Poltergeist

2019-02-17 06:39发布

问题:

I'm trying to open a link by clicking on it using capybara/poltergeist which opens up in a new tab.

I can't seem to get it working.

@session.find(<link>).click

just seems to stay on the same page, as does

click_link "<link>"

@session.driver.window_handles 

Only seems to give me 1 window whatever I'm doing.

How do I get that link to open in a new tab?

Seems to be a fair amount of confusion as to what works or doesn't a la (With Capybara, how do I switch to the new window for links with "_blank" targets?).

Hoping somebody has worked it out... Can't seem to get my head around it.

Ok. By updating to the latest version of poltergeist (1.6.0) we have some progress.

Now however I have two windows but no idea how to switch between them.

@session.windows

gives me

[Window @handle="0", Window @handle="1"] (slightly modified as it was going a bit funny).

But

@session.switch_to_window(1)

results in

NoMethodError: undefined method `handle' for "1":String

回答1:

Right, as of Feb 5th 2014 and v1.6 of poltergeist, you can do this, as ...

@session.click_on "link_with_target _blank"

@session.switch_to_window(@session.windows.last)
#Do whatever you're doing
@session.current_window.close
@session.switch_to_window(@session.windows.first)

Hope that clears things up for somebody...



回答2:

I did this with

page.switch_to_window(page.windows[0])

Maybe that's useful to someone.