switch window using handle in python

2,083 views
Skip to first unread message

dolunay

unread,
Nov 30, 2011, 6:04:59 PM11/30/11
to Selenium Users
Hello:

How do I switch windows using handles? switch_to_window() does not
work since it needs name. [self.execute(Command.SWITCH_TO_WINDOW,
{'name': window_name})]

#webdriver.__version__
#'2.11.0'
b=webdriver.Firefox()
b.current_window_handle
u'{fe621e9c-b0b4-4649-9b90-16badf98f6e9}'
hs = b.window_handles

Then ??

Thanks.

Luke Inman-Semerau

unread,
Nov 30, 2011, 7:02:08 PM11/30/11
to seleniu...@googlegroups.com
If you have more than one window and you switch to one of the other ones

s = set(b.window_handles)
s.remove(b.current_window_handle)
b.switch_to_window(s.pop())

-Luke

> --
> You received this message because you are subscribed to the Google Groups "Selenium Users" group.
> To post to this group, send email to seleniu...@googlegroups.com.
> To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.
>

dolunay

unread,
Dec 1, 2011, 1:30:39 PM12/1/11
to Selenium Users
Thanks for your answer. However, I get the following error after using
swithc_to_window

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/
remote/webdriver.py", line 487, in switch_to_window
self.execute(Command.SWITCH_TO_WINDOW, {'name': window_name})
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/
remote/webdriver.py", line 151, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/
remote/remote_connection.py", line 267, in execute
return self._request(url, method=command_info[0], data=data)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/
remote/remote_connection.py", line 308, in _request
response = opener.open(request)
File "/usr/lib/python2.7/urllib2.py", line 394, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 412, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1201, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1174, in do_open
r = h.getresponse(buffering=True)
File "/usr/lib/python2.7/httplib.py", line 1027, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 407, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.7/httplib.py", line 371, in _read_status
raise BadStatusLine(line)
httplib.BadStatusLine: ''


Any ideas?


On Nov 30, 4:02 pm, Luke Inman-Semerau <luke.seme...@gmail.com> wrote:
> If you have more than one window and you switch to one of the other ones
>
> s = set(b.window_handles)
> s.remove(b.current_window_handle)
> b.switch_to_window(s.pop())
>
> -Luke
>

Kenneth Koontz

unread,
Dec 1, 2011, 2:20:58 PM12/1/11
to Selenium Users
I'm assuming you are using selenium 2.8-2.12. Can you verify the
version of selenium that you are using? Grab the latest version of
selenium if your version is within the mentioned version numbers.

Luke Inman-Semerau

unread,
Dec 1, 2011, 2:22:26 PM12/1/11
to seleniu...@googlegroups.com
Can you let me know what browser version / os / selenium version you are using and a url or sample html that reproduces this?

dolunay

unread,
Dec 2, 2011, 3:29:02 AM12/2/11
to Selenium Users
I had both 2.11 and 2.12 selenium on different machines. I installed
2.14 and don't see that issue.

Luke and Kenneth: Thank you for your time.

On Dec 1, 11:22 am, Luke Inman-Semerau <luke.seme...@gmail.com> wrote:
> Can you let me know what browser version / os / selenium version you are
> using and a url or sample html that reproduces this?
>

Reply all
Reply to author
Forward
0 new messages