简体   繁体   English

在Mac上使用Selenium WebDriver在Firefox中打开新标签页

[英]Open new tab in Firefox using Selenium WebDriver on Mac

I just installed Selenium Web Driver and tried it out. 我刚刚安装了Selenium Web Driver并进行了尝试。 It works great. 效果很好。 My use case can be describe as followed: 我的用例可以描述如下:

  1. Start Firefox on a server with pseudo X server (Xvfb) 在具有伪X服务器(Xvfb)的服务器上启动Firefox
  2. New Driver.Firefox() object 新的Driver.Firefox()对象
  3. Open 10 tabs and load a webpage in each tab 打开10个标签并在每个标签中加载网页
  4. Retrieve the html from all loaded pages 从所有加载的页面检索html

The only step that is not working is step 3. I can not find out how to open new tabs. 唯一不起作用的步骤是步骤3。我无法找到如何打开新标签页。 I found this here on SO : How to open a new tab using Selenium WebDriver with Java? 我在SO上找到了这一点: 如何使用带有Java的Selenium WebDriver打开新选项卡? However, I tested this locally (ie with visible display) on my Mac for debugging purpose and I saw that the Firefox browser (which was opened when creating the driver object) does not open any tabs when doing as described on the SO thread. 但是,我在Mac上进行了本地测试(即显示可见)以进行调试,并且发现Firefox浏览器(在创建驱动程序对象时打开)在按SO线程中的说明进行操作时未打开任何选项卡。 So I tried this here: 所以我在这里尝试了这个:

driver = webdriver.Firefox()
driver.get("https://stackoverflow.com/")
body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')

As I said, it does not work for me. 正如我所说,这对我不起作用。 So, how else is it possible to open tabs? 那么,还有什么可能打开标签页呢? I use Selenium 2.39 (pip install selenium) and Python 2.7. 我使用Selenium 2.39(点安装Selenium)和Python 2.7。

在OSX上打开新选项卡的组合键是Command + T,因此您应该使用

body.send_keys(Keys.COMMAND + 't') 

It's probably slightly more correct to send it to the browser via action chaining since you're not actually typing text; 通过操作链接将其发送到浏览器可能更正确,因为您实际上没有在键入文本。 this also makes your code more readable imo 这也使您的代码更易读imo

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

# before correction from  DMfll:
# ActionChains(driver).send_keys(Keys.COMMAND, "t").perform()

# correct method
ActionChains(driver).key_down(Keys.COMMAND).send_keys("t").key_up(Keys.COMMAND)‌​‌​.perform()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python中使用Selenium WebDriver在新标签页/窗口中打开链接 - Using Selenium WebDriver in Python to Open Link in New Tab/Window 使用 Selenium WebDriver 的新标签 - New Tab using Selenium WebDriver Python + Selenium WebDriver:在新标签页中打开URL - Python + Selenium WebDriver: open URL in new tab 在 Mac 上使用 Python 和 Firefox 的 selenium webdriver 问题 - Problems with selenium webdriver using Python and Firefox on a Mac 如何使用 Control + Click of Selenium Webdriver 在同一窗口的新选项卡中的主选项卡中打开嵌入在 webelement 中的链接 - How to open a link embeded in a webelement with in the main tab, in a new tab of the same window using Control + Click of Selenium Webdriver 硒中的webdriver在FireFox中打开一个新的Internet Explorer选项卡 - webdriver in selenium opens a new Internet Explorer tab in FireFox 如何修复 Selenium Webdriver 无法在 Firefox 68.0 及更高版本上打开新标签? - How to fix Selenium Webdriver not opening a new tab on Firefox 68.0 and above? 在 OS X 中使用 Selenium WebDriver 打开和关闭新标签页 - Open and close new tab with Selenium WebDriver in OS X Python - 使用 Selenium WebDriver 在新的 Chrome 选项卡中打开链接? - Python - Open a link in new Chrome tab with Selenium WebDriver? Selenium webdriver在python中的chrome编码中打开一个新选项卡 - Selenium webdriver open a new tab in chrome coding in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM