Sendkeys 在使用 Appium 的 Winappdriver 中不起作用
Posted
技术标签:
【中文标题】Sendkeys 在使用 Appium 的 Winappdriver 中不起作用【英文标题】:Sendkeys not working in Winappdriver using Appium 【发布时间】:2020-07-14 03:55:39 【问题描述】:我正在使用 C# 语言的 Appium 使用 WinAppDriver 自动化桌面应用程序。我面临的问题是键盘交互的代码都不起作用,例如 setText、keys.ArrowDown 等。最初,它工作得很好,但从过去几天开始,它完全停止工作。 谁能建议怎么做?
using OpenQA.Selenium;
using OpenQA.Selenium.Appium.Windows;
using System;
namespace UITest
public class Test
public string Name get; set; = string.Format("A_EN_0", DateTime.Now.Ticks.ToString());
public string Description get; set; = string.Format("A_ED_0", DateTime.Now.Ticks.ToString());
private readonly Records records = null;
public Test()
records = new Records();
public void SetValue(string fieldName, string value = "", bool pressTab = true)
string propValue;
if (!string.IsNullOrWhiteSpace(value))
propValue = value;
else
propValue = Common.GetPropValue(this, fieldName).ToString();
switch (fieldName)
case "Name":
Common.RE7Session.FindElementByAccessibilityId("30").SendKeys(propValue);
break;
case "Description":
Common.RE7Session.FindElementByAccessibilityId("18").SendKeys(propValue);
break;
if (pressTab)
Common.SendKeys(Keys.Tab);
public void AddEvent(Common.SaveOptions saveOptions = Common.SaveOptions.None)
Common.RE7Session.OpenFunctionalArea("Records");
records.OpenTask("Events");
Common.RE7Session.FindElementByName("New Event").Click();
Common.RE7Session.SwitchWindowWithWait(3);
this.SetValue(nameof(this.Name));
this.SetValue(nameof(this.Description));
Common.RE7Session.SetText("test");
Common.SendKeys(Keys.ArrowDown);
Common.RE7Session.PressTab();
Common.RE7Session.SetText(new Random().Next(10000, 99999).ToString(), false);
在这里,在最后一行代码中,SetText、PressTab、Key.ArrowDown,这些都不起作用。 PressTab、SendKeys的方法如下:
public static void PressTab(this WindowsDriver<WindowsElement> RE7Session, int iteration = 1)
for (int i = 0; i < iteration; i++)
Common.SendKeys(Keys.Tab);
public static void SendKeys(string keysToSend)
System.Windows.Forms.SendKeys.SendWait(ReplaceKeyCode(keysToSend));
Sleep(100);
private static string ReplaceKeyCode(string keys)
keys = keys.Trim('+');
//Action characters like tab, arrow down and etc
keys = keys.Replace(Keys.Backspace, "BACKSPACE");
keys = keys.Replace(Keys.Delete, "DELETE");
keys = keys.Replace(Keys.ArrowDown, "DOWN");
keys = keys.Replace(Keys.End, "END");
keys = keys.Replace(Keys.Enter, "ENTER");
keys = keys.Replace(Keys.Escape, "ESC");
keys = keys.Replace(Keys.Help, "HELP");
keys = keys.Replace(Keys.Home, "HOME");
keys = keys.Replace(Keys.Insert, "INSERT");
keys = keys.Replace(Keys.ArrowLeft, "LEFT");
keys = keys.Replace(Keys.PageDown, "PGDN");
keys = keys.Replace(Keys.PageUp, "PGUP");
keys = keys.Replace(Keys.ArrowRight, "RIGHT");
keys = keys.Replace(Keys.Tab, "TAB");
keys = keys.Replace(Keys.ArrowUp, "UP");
keys = keys.Replace(Keys.F1, "F1");
keys = keys.Replace(Keys.F2, "F2");
keys = keys.Replace(Keys.F3, "F3");
keys = keys.Replace(Keys.F4, "F4");
keys = keys.Replace(Keys.F5, "F5");
keys = keys.Replace(Keys.F6, "F6");
keys = keys.Replace(Keys.F7, "F7");
keys = keys.Replace(Keys.F8, "F8");
keys = keys.Replace(Keys.F9, "F9");
keys = keys.Replace(Keys.F10, "F10");
keys = keys.Replace(Keys.F11, "F11");
keys = keys.Replace(Keys.F12, "F12");
keys = keys.Replace(Keys.Add, "ADD");
keys = keys.Replace(Keys.Subtract, "SUBTRACT");
keys = keys.Replace(Keys.Multiply, "MULTIPLY");
keys = keys.Replace(Keys.Divide, "DIVIDE");
//Special Keys like control, shift and alt
keys = keys.Replace(Keys.Control, "^");
keys = keys.Replace(Keys.LeftControl, "^");
keys = keys.Replace(Keys.Shift, "+");
keys = keys.Replace(Keys.LeftShift, "+");
keys = keys.Replace(Keys.Alt, "%");
keys = keys.Replace(Keys.LeftAlt, "%");
return keys;
库中发生了一些更改,导致直接键.[key] 不起作用。因此,我使用上述 relaceKeyCode 方法对键进行了更改。但是,这仍然行不通。
【问题讨论】:
你能解决这个问题吗? 【参考方案1】:也许这会有所帮助:https://github.com/Microsoft/WinAppDriver/issues/134
简而言之,SendKeys(Keys.Contro + "a").,例如,e 按下控制键但不释放它。如果要释放控制键,则需要像 SendKeys(Keys.Control + "a" + Keys.Control) 一样重复。
写得好,繁荣昌盛!
【讨论】:
以上是关于Sendkeys 在使用 Appium 的 Winappdriver 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
在 Appium Sendkeys 中没有按预期工作,它发送的比我给它的多