Python 使用硒发送多个选项卡按键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35385221/
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 multiple tab key presses with selenium
提问by pinas
How can I send multiple tabs with Selenium?
如何使用 Selenium 发送多个标签?
When I run:
当我运行时:
uname = browser.find_element_by_name("text")
uname.send_keys(Keys.TAB)
the next element is selected. When executing uname.send_keys(Keys.TAB)
again nothing happens - actually the next element from uname
is selected -> so it is the same as when running it once.
下一个元素被选中。uname.send_keys(Keys.TAB)
再次执行时什么也没有发生 - 实际上选择了下一个元素uname
-> 所以它与运行一次时相同。
How can I jump forward multiple times - basically as I would press the TAB manually multiple times?
我怎样才能多次向前跳跃 - 基本上就像我多次手动按下 TAB 一样?
采纳答案by alecxe
Use Action Chains:
使用动作链:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
N = 5 # number of times you want to press TAB
actions = ActionChains(browser)
for _ in range(N):
actions = actions.send_keys(Keys.TAB)
actions.perform()
Or, since this is Python, you can even do:
或者,由于这是 Python,您甚至可以执行以下操作:
actions = ActionChains(browser)
actions.send_keys(Keys.TAB * N)
actions.perform()
回答by SyedElec
I think you can also write
我觉得你也可以写
uname.send_keys(Keys.TAB + Keys.TAB + Keys.TAB + ... )
It may be useful if you have only 2 or 3 commands to send.
如果您只有 2 或 3 个命令要发送,这可能很有用。
回答by VRK
uname.send_keys(Keys.TAB,Keys.TAB,Keys.TAB..)
worked for me
为我工作
回答by jcomeau_ictx
As the OP states: "actually the next element from uname
is selected".
正如 OP 所说:“实际上选择了下一个元素uname
”。
After the first <TAB>
key you have moved off the element, so no further <TAB>
s will be recognized by that element. You need to locate the parentelement and send keys to it.
第一后<TAB>
关键你已经移出了元素,所以没有进一步的<TAB>
旨意来识别该元素。你需要找到父元素和发送键它。