简体   繁体   English

使用不带对象的WebDriver / Seleneium输入击键以将命令发送到

[英]Entering Keystrokes with WebDriver/Seleneium without an Object to send the command to

I am having trouble figuring out how to switch to a popup window built within a webpage and the easy way around that fix is to just hit the "enter" key. 我在弄清楚如何切换到内置在网页中的弹出窗口时遇到麻烦,解决此问题的简单方法是按“输入”键。 However, the way the class works is that the command: 但是,该类的工作方式是命令:

Sendkeys.Keys("<insert Key>");

Expects there to be some type of textbox to send it to (unless I do not understand it well enough), where I just need to hit the enter key, as if just stroking it on the keyboard. 期望会有某种类型的文本框发送给它(除非我不太了解它),在这里我只需要按Enter键,就像在键盘上轻敲它一样。 Is this possible in Selenium or the Windows namespaces? 在Selenium或Windows名称空间中可能吗?

EDIT 编辑

I did not have the code in which I am working with, so I will show it now: 我没有正在使用的代码,因此现在将显示它:

driver.FindElement(By.Id("ctl00_c_btnPost_btn")).Click();

This command forces the window to pop up. 此命令将强制弹出窗口。 To be clear, this is not another window in my browser, it is a window that pops up within the webpage, which is where my problem came from. 需要明确的是,这不是我浏览器中的另一个窗口,而是一个在网页中弹出的窗口,这就是我的问题所在。 The form looks like: 该表格如下所示:

在此处输入图片说明

After this window pops up, I want to hit the enter key to simply select "OK," without have to actually select the window. 弹出该窗口后,我想按Enter键只是选择“确定”,而不必实际选择该窗口。 However, I would love someone to explain how to select the window as well. 但是,我也希望有人能解释如何选择窗口。

A common approach is to send keys to the body element: 一种常见的方法是将密钥发送到body元素:

driver.FindElement(By.TagName("body")).SendKeys("Keys here");

Though, if this is an alert, switch to it and accept it : 不过,如果这是一个警报,请切换到该警报并接受它

IAlert alert = driver.SwitchTo().Alert();
alert.Accept();

You can also send the keys to that "window wrapper" popup you've shown on the screenshot: 您还可以将密钥发送到屏幕截图中显示的“窗口包装”弹出窗口:

driver.FindElement(By.CssSelector("div[id^=RadWindowWrapper]")).SendKeys(Keys.RETURN);

Or, locate the OK button inside and click it: 或者,找到其中的“确定”按钮并单击它:

driver.FindElement(By.Xpath("//div[starts-with(@id, 'RadWindowWrapper')]//*[. = 'OK']")).Click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM