2014-02-14 85 views
0

我刚安装了Selenium Web Driver并试用了它。它效果很好。我用例可以描述如下:在Mac上使用Selenium WebDriver在Firefox中打开新选项卡

  1. 启动Firefox与伪X服务器(Xvfb来)
  2. 新Driver.Firefox()对象
  3. 打开10个标签页的服务器上,并在每个选项卡中加载网页
  4. 检索从一个没有工作的所有加载页面

只有一步HTML是第3步我无法找出如何打开新标签页。我在SO上发现了这个:How to open a new tab using Selenium WebDriver with Java?但是,为了调试的目的,我在本地测试了这个(即用可见显示器),并且我看到Firefox浏览器(在创建驱动程序对象时打开)在打开时没有打开任何选项卡如SO线程中所述。所以我在这里试过这个:

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

正如我所说,它不适合我。那么,如何才能打开制表符?我使用Selenium 2.39(pip install selenium)和Python 2.7。

回答

3

的组合键,打开在OSX上一个新的选项卡命令+ T,所以你应该使用

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

是的,没错。但是,这是有趣的混乱:)。谢谢! – toom

+1

@toom:要清楚的是,由于苹果是这么做的,所以Selenium只是遵循那里的内容而已。 –

1

这可能稍微正确的通过动作将其发送到浏览器链接,因为你没有实际输入文字;这也让你的代码更具可读性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() 
+1

我没有完全从ActionChains(driver).send_keys(Keys.COMMAND,“t”)。perform()'得到预期的行为。这给了我预期的行为,如下所述:[http://selenium-python.readthedocs.org/en/latest/api.html#module-selenium.webdr iver.common.action_chains](http:// selenium- python.readthedocs.org/en/latest/api.html#module-selenium.webdr iver.common.action_chains):'ActionChains(driver).key_down(Keys.COMMAND).send_keys(“t”)。key_up(Keys .COMMAND).perform()'。它包括一个键和一个键。 – DMfll

+1

谢谢,DMfll。我更新了我的答案 – user2426679

相关问题