使用 Python 绑定在 Selenium 中发送键控制 + 单击 [英] Send keys control + click in Selenium with Python bindings

查看:27
本文介绍了使用 Python 绑定在 Selenium 中发送键控制 + 单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Selenium 在新标签页中打开链接.

I need to open link in new tab using Selenium.

那么是否可以在 Selenium 中对元素执行 ctrl+click 以在新选项卡中打开它?

So is it possible to perform ctrl+click on element in Selenium to open it in new tab?

推荐答案

使用 ActionChainkey_down 按下控制键,key_up> 释放它:

Use an ActionChain with key_down to press the control key, and key_up to release it:

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

driver = webdriver.Chrome()

driver.get('http://google.com')
element = driver.find_element_by_link_text('About')

ActionChains(driver) 
    .key_down(Keys.CONTROL) 
    .click(element) 
    .key_up(Keys.CONTROL) 
    .perform()

time.sleep(10) # Pause to allow you to inspect the browser.

driver.quit()

这篇关于使用 Python 绑定在 Selenium 中发送键控制 + 单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆