当前位置: 首页 > 知识库问答 >
问题:

无法使用python中的selenium web驱动程序发送密钥[duplicate]

林龙野
2023-03-14

我是Python的新手,我只是想写一个代码在浏览器中打开新标签。所以我寻找一些方法来做到这一点,我遇到了这个过程。但是,尽管它在youtube视频中运行良好,我的却不能。它只是打开了一个新的窗口,并前往google.com.But,它没有按下ctrl+t打开一个新的标签。我不知道为什么,因为它甚至没有在python外壳中显示任何错误。我希望有人能帮我解决这个问题,告诉我我的代码有什么问题。谢谢你

#! python3

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser=webdriver.Firefox()
browser.get('http://www.google.com')
elm=browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t')
time.sleep(2)
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL+Keys.PAGE_DOWN)

共有1个答案

养枫涟
2023-03-14

这是在硒中打开一个新选项卡的代码:

import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from time import sleep    

browser = webdriver.Firefox()
browser.get('https://www.google.com?q=python#q=python')
first_result = ui.WebDriverWait(browser, 15).until(lambda browser: browser.find_element_by_class_name('rc'))
first_link = first_result.find_element_by_tag_name('a')

# Save the window opener (current window, do not mistaken with tab... not the same)
main_window = browser.current_window_handle

# Open the link in a new tab by sending key strokes on the element
# Use: Keys.CONTROL + Keys.SHIFT + Keys.RETURN to open tab on top of the stack 
first_link.send_keys(Keys.CONTROL + Keys.RETURN)

# Switch tab to the new tab, which we will assume is the next one on the right
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

# Put focus on current window which will, in fact, put focus on the current visible tab
browser.switch_to_window(main_window)

# do whatever you have to do on this page, we will just got to sleep for now
sleep(2)

# Close current tab
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')

# Put focus on current window which will be the window opener
browser.switch_to_window(main_window)
 类似资料:
  • 我正在尝试登录到通过selenium。不知何故,我无法输入我的凭据。这是我的密码。 我得到的错误是 异常在线程"main"org.openqa.selenium.Timeout异常:预期条件失败:等待可见性的元素位于By.xpath:后代::*[@class='凭据']/后代::输入[1](尝试60秒(s)以500毫秒间隔)构建信息:版本:'4.1.0',修订:'87802e897b'系统信息:主

  • 问题内容: 我试图单击此页面上的元素: 在这一点上,我想单击“现金流量”,“资产负债表”或“季度”。我知道这些按钮已加载,因为我可以使用BeautifulSoup从页面源中提取它们。但是,当我尝试使用Selenium时: 全部返回“无法定位元素”,除了“季度”返回一个元素,但它位于图上方的一个元素而不是我感兴趣的表格上方的一个元素。 我认为这是由于位于错误的iframe中,而我找到了所有ifram

  • 有时还会发出以下警告: 我尝试更改一些集群对象参数,但没有帮助。 以下是我正在使用的cassandra中的密钥空间配置:

  • 我已经能够通过外部web应用程序使用oauth rest api访问azure key vault,但出于某种原因,我无法从密钥中检索机密。经过长时间的研究,我发现用powershell和C#可以做到这一点,但还没有找到用Python解决方案。有人知道python是否有可能,或者有一种方法可以模拟powershell正在做什么吗?以下是检索秘密的代码: 此外,我已经在azure portal中注册

  • 代码: 输出: Traceback(最近一次调用last):文件“C:/Users/RV CSP/pycharm projects/untitled 25/hotel . py”,第9行,在session = cluster.connect('dbsi ')文件“C:\ Users \ RV CSP \ AppData \ Roaming \ Python \ Python 36 \ site-p

  • 我试图使用Selenium Webdriver python单击网站上的一个按钮,我看到该按钮高亮显示,但没有使用函数单击。 下面是我尝试单击的按钮的html代码 我正在尝试使用id单击按钮 我还尝试了xpath,得到了相同的结果