Forum: Ruby Can we open a page in a new tab in the same browser using selenium-webdriver?

Posted by Love U Ruby (my-ruby)
on 2013-02-03 19:31
driver = Selenium::WebDriver.for :firefox
driver.get "https://www.example.com/"

driver.find_element(:link_text, "empid").click

It is opening the page in the current tab.

Any chance to open this link to another tab of the same browser?


And when done some processing and close that new tab, without exiting
the full browser?

Thanks
Posted by Joel Pearson (virtuoso)
on 2013-02-03 20:39
I highly recommend watir-webdriver, selenium's API just feels so clumsy
in comparison, and it's the same underlying driver...

As far as I know, tabs are not supported in webdriver.
However, here's an alternative:

driver = Selenium::WebDriver.for :firefox
driver.get 'http://www.google.com'

#Extract the link we want to go to
address = driver.find_element(:link_text, "Gmail").attribute('href')

#Open a new browser window
driver.execute_script( "window.open()" )

#Use the newest window
driver.switch_to.window( driver.window_handles.last )

#Navigate to the url we just extracted
driver.get address

#Once we're done with that window:
driver.close
driver.switch_to.window( driver.window_handles.first )
Posted by Love U Ruby (my-ruby)
on 2013-02-03 21:27
Joel Pearson wrote in post #1095031:
> I highly recommend watir-webdriver, selenium's API just feels so clumsy
> in comparison, and it's the same underlying driver...
>
> As far as I know, tabs are not supported in webdriver.
> However, here's an alternative:
>

@Joel - > Thanks for your help :)
Posted by Joel Pearson (virtuoso)
on 2013-02-03 23:30
Actually, come to think of you can skip a step:

driver.execute_script( "window.open('#{address}')" )

That negates the need to use "driver.get address", although I'm not sure 
how webdriver will handle the page load waiting using that method.
Posted by Love U Ruby (my-ruby)
on 2013-04-22 18:57
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.get "https://www.google.co.in/"
address = driver.find_element(:link_text, "Gmail").attribute('href')
driver.execute_script( "window.open()" )
p driver.window_handles.first #=> 
"{f17eac79-daf9-4a6c-a1ff-1b524fef9faf}"
driver.switch_to.window( driver.window_handles.last )
driver.get address
driver.execute_script "window.close()"
# driver.execute_script "window.close()"
p driver.window_handles.first #=> 
"{f17eac79-daf9-4a6c-a1ff-1b524fef9faf}"
driver.switch_to.window( driver.window_handles.first )
p driver.title #=> "Google"
driver.close #why does here this browser not getting closed?
Posted by Joel Pearson (virtuoso)
on 2013-04-22 23:28
Works for me. Are you sure you didn't have another window open?
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.