Rinktacular
Rinktacular

Reputation: 1126

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. Is this possible in Selenium or the Windows namespaces?

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:

enter image description here

After this window pops up, I want to hit the enter key to simply select "OK," without have to actually select the window. However, I would love someone to explain how to select the window as well.

Upvotes: 1

Views: 1269

Answers (1)

alecxe
alecxe

Reputation: 473893

A common approach is to send keys to the body element:

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();

Upvotes: 1

Related Questions