刚才我遇到了同样的问题,在我看来,我发现一种方法比尝试将密钥发送到PhantomJS更好。
请记住,PhantomJS是一款无头浏览器 - 您的操作系统无法通过键盘快捷键访问实际窗口。这就是说,每次打开一个新的标签页/窗口时,它都会添加到驱动程序的窗口句柄中。每个窗口句柄都有唯一的标识符。
你可以很容易地切换到该窗口的ID(并返回到你原来的窗口处理程序 - 如果你想)。
例:
# Click a link that opens a new tab ...
# You'll see there's a new window handle!
print(driver.window_handles)
# Switch to the new window handle in the window_handles list
driver.switch_to.window(driver.window_handles[1])
# Switch back to the original window
driver.switch_to.window(driver.window_handles[0])
然后是微不足道的只是检查驾驶员的current_url
,以确保你在正确的页面上,即:
assert "www.stackoverflow.com" in driver.current_url
这个答案让我:https://stackoverflow.com/a/29125205/295246
Cuz PhantomJS是JavaScript动力,你可以随时打开一个新的窗口与'driver.execute_script('window.open(“http:/ /your-url.dot“)');'代替”点击打开新标签的链接“。仅仅依靠网址命名你的手柄也更好,但也可以。 – erm3nda
这实际上并没有回答如何打开新窗口,并且像我说的那样通过JavaScript做的确会导致在功能中丢失以前的userAgent(以及更多的事情)。我认为附加一个虚拟的'target ='_ blank''链接并点击它将是一个很好的方法。 – erm3nda