[英]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: 我的用例可以描述如下:
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.