使用 Python 绑定在 Selenium 中发送键控制 + 单击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27775759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send keys control + click in Selenium with Python bindings
提问by micgeronimo
I need to open link in new tab using Selenium.
我需要使用 Selenium 在新选项卡中打开链接。
So is it possible to perform ctrl+click on element in Selenium to open it in new tab?
那么是否可以在 Selenium 中对元素执行 ctrl+click 以在新选项卡中打开它?
采纳答案by Louis
Use an ActionChain
with key_down
to press the control key, and key_up
to release it:
使用ActionChain
withkey_down
按下控制键,然后key_up
松开:
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()
回答by Rupesh Shinde
Below is what i have tried for Selenium WebDriver with Java binding and its working for me. If you want to manually open the Link in New Tab you can achieve this by performing Context Click on the Link and selecting 'Open in new Tab' option. Below is the implementation in Selenium web-driver with Java binding.
以下是我尝试使用 Java 绑定的 Selenium WebDriver 及其对我的工作。如果您想在新选项卡中手动打开链接,您可以通过执行上下文单击链接并选择“在新选项卡中打开”选项来实现此目的。下面是带有 Java 绑定的 Selenium Web 驱动程序的实现。
Actions newTab= new Actions(driver);
WebElement link = driver.findElement(By.xpath("//xpath of the element"));
//Open the link in new window
newTab.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
Web-driver handles new tab in the same way to that of new window. You will have to switch to new open tab by its window name.
Web 驱动程序以与新窗口相同的方式处理新选项卡。您必须通过其窗口名称切换到新打开的选项卡。
driver.switchTo().window(windowName);
You can keep track of window-names which will help you to easily navigate between tabs.
您可以跟踪窗口名称,这将帮助您轻松地在选项卡之间导航。
回答by aberna
Two possible solutions:
两种可能的解决方案:
opening a new tab
打开一个新标签
self.driver = webdriver.Firefox()
self.driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
this is the solution for MAC OSX. In other cases you can use the standard Keys.CONTROL + 't'
这是 MAC OSX 的解决方案。在其他情况下,您可以使用标准 Keys.CONTROL + 't'
opening a new webdriver
打开一个新的网络驱动程序
driver = webdriver.Firefox() #1st window
second_driver = webdriver.Firefox() #2nd windows
回答by Helping Hands
Following is working for me to open link in new tab :
以下是我在新选项卡中打开链接的工作:
String link = Keys.chord(Keys.CONTROL,Keys.ENTER);
driver.findElement(By.linkText("yourlinktext")).sendKeys(link);
Above code is in java. you can convert to python easily I assume.
上面的代码是在java中。我假设您可以轻松转换为python。
Please ask if have any query.
请询问是否有任何疑问。
回答by Satheesh Kumar
By importing Keys Class, we can open page in new tab or new window with CONTROL or SHIFT and ENTER these keys:
driver.find_element_by_xpath('//input[@name="login"]').send_keys(Keys.CONTROL,Keys.ENTER)
通过导入 Keys Class,我们可以使用 CONTROL 或 SHIFT 在新选项卡或新窗口中打开页面,然后输入这些键:
driver.find_element_by_xpath('//input[@name="login"]').send_keys(Keys.CONTROL,Keys.ENTER)
or
或者
driver.find_element_by_xpath('//input[@name="login"]').send_keys(Keys.SHIFT,Keys.ENTER)
driver.find_element_by_xpath('//input[@name="login"]').send_keys(Keys.SHIFT,Keys.ENTER)