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

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

问题描述

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

I need to open link in new tab using Selenium.

那么可以按Ctrl键并单击Selenium中的元素以在新选项卡中将其打开吗?

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天全站免登陆