无法使用C#和Selenium发送符号或花括号

时间:2014-08-18 08:15:36

标签: c# internet-explorer selenium webdriver

基本上我想将以下文字放入HTML input字段:{mail@example.com}

不幸的是:elem.SendKeys("{mail@example.com}")会产生7mailqexample.com0

在我的德语键盘布局上,7,q和0对应正确的键,但是,看起来API不会事先发送AltGr键。我已经读过不同的键盘布局存在问题,这也是我测试以下代码的原因:

elem.SendKeys (Keys.LeftAlt + "Q"); // Is LeftAlt the "normal" Alt?
elem.SendKeys (Keys.Alt + "Q"); // Alt is AltGr? 
elem.SendKeys (Keys.LeftAlt + "2"); // Try also to type '@' with English layout
elem.SendKeys (Keys.Alt + "2");

不幸的是,这四个调用导致空输入控件。

解决方案是什么?

旁注:这只发生在使用InternetExplorerDriver(我只使用32位驱动程序)(启用本机事件)。 ChromeDriver能够完美地使用elem.SendKeys("@") ...

另一方面注意:这可能是IEDriver实施中的错误吗?改为使用以下代码段时:

new Actions(webDriver).MoveToElement(elem).Click().Perform(); // Focus input element
SendKeys.SendWait("@");

它有效......但是,这看起来并不正确...

3 个答案:

答案 0 :(得分:1)

近一年前,您遇到了InternetExplorerDriver I've reported的问题。

引用自己的问题:

  

当要求窗口焦点设置为true时使用本机事件时,   IE-Driver将使用SendInput而不是SendMessage API(根据   http://jimevansmusic.blogspot.com/2013/01/revisiting-native-events-in-ie-driver.html

     

似乎IE驱动程序正在寻找键盘组合   设置键盘布局(德语),但输入此组合   英文键盘布局,或类似的东西。

可能的解决方法:

  • 使用英文键盘布局
  • 将requireWindowFocus设置为false(我自己没有尝试过)
  • 对我报告的问题发表评论和评论;也许它很快就会得到解决:)

答案 1 :(得分:0)

尝试     Key.OemCloseBrackets 要么     Key.OemOpenBrackets

答案 2 :(得分:0)

我在软件和质量保证Stackexchange网站How can I send the action “Ctrl” + “Alt” + “Double Click” to the browser

中找到了一个解决方案

基本上你必须创建一个Actions对象,添加所需的键功能,KeyUp所有按下的键,最后Perfom()个动作。

使用英文键盘配置,您必须:

var actions = new OpenQA.Selenium.Interactions.Actions(driver);
actions.KeyDown(Keys.Control).KeyDown(Keys.Alt);
actions.SendKeys("2"); // '@' char
//actions.SendKeys("]"); // '}´ çhar
actions.KeyUp(Keys.Control).KeyUp(Keys.Alt);
actions.Build().Perform();