Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

If the window closes too quickly, switch_to_window is invalid #1435

Answered by mdmintz
chenqinggang001 asked this question in Q&A
Discussion options

If the window closes too quickly, switch_to_window is invalid
I have to wait a few seconds

I can't predict how long I will have to wait for my next step, so can there be a waiting period like timeout

self.click("close", timeout=10, delay=1)
self.sleep(3)
self.switch_to_window(-1)
You must be logged in to vote

You might be able to close the active window/tab directly, and then switch to the most recent one after that:

self.driver.close()
self.switch_to_window(-1)

Replies: 1 comment 3 replies

Comment options

You might be able to close the active window/tab directly, and then switch to the most recent one after that:

self.driver.close()
self.switch_to_window(-1)
You must be logged in to vote
3 replies
Comment options

On second thought, my idea is impractical, but my business logic is to open a new page and select a portion of the content, which requires interaction with the original page through the close button on the new page, and closing it directly doesn't work

Comment options

I can't guess what to wait for (or how long) without knowing more information. You'll have to figure that out on your own.

Comment options

@mdmintz I wrote a use case using the demo page

from seleniumbase import BaseCase
class MyTestClass(BaseCase):
 def test_demo(self):
 self.open("https://seleniumbase.io/realworld/login")
 self.click("//h6[@align='center']//a")
 # Here's an example: After I've finished registering from here, I need to click the close button on a page
 # that says "You have successfully registered."
 self.click("//button[@class='close']") # Suppose there is a close button on the page
 # If I just use `switch_to_window`
 self.switch_to_window(-1)
 # The following lookup action takes place in https://seleniumbase.io/realworld/signup
 self.find_element("//h4[@class='auth-header']//font//font")

Like this:

from seleniumbase import BaseCase
class MyTestClass(BaseCase):
 def test_demo(self):
 self.open("https://seleniumbase.io/realworld/login")
 self.click("//h6[@align='center']//a")
 # Here's an example: After I've finished registering from here, I need to click the close button on a page
 self.driver.close()
 # If I just use `switch_to_window`
 self.switch_to_window(-1)
 # The following lookup action takes place in https://seleniumbase.io/realworld/signup
 self.find_element("//h4[@class='auth-header']//font//font")
Answer selected by mdmintz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /