chr*_*ris 4 python selenium phantomjs
我正在使用Selenium和PhantomJS webdriver,我发现我无法使用这个webdriver打开一个新标签.我正在使用标准线:
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
Run Code Online (Sandbox Code Playgroud)
我用的是Mac.
我也尝试过使用Firefox,它可以工作.
任何有助于它与PhantomJS合作的帮助将不胜感激!
我刚才遇到了同样的问题,在我看来,我找到了一种比尝试向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])
Run Code Online (Sandbox Code Playgroud)
然后检查驱动程序current_url以确保您在正确的页面上是微不足道的,即:
assert "www.stackoverflow.com" in driver.current_url
Run Code Online (Sandbox Code Playgroud)
这个答案对我有帮助:https://stackoverflow.com/a/29125205/295246