I am trying to send keys to an element within an iFrame. but it does not seem to be inputting any characters into the input.
IWebElement email = driver.FindElement(By.Name("email"));
email.SendKeys("someemail@input.com");
I am trying to send keys to an element within an iFrame. but it does not seem to be inputting any characters into the input.
IWebElement email = driver.FindElement(By.Name("email"));
email.SendKeys("someemail@input.com");
How about pasting to the element:
IWebElement email = driver.FindElement(By.Name("email"));
//email.SendKeys("someemail@input.com");
Clipboard.SetText("someemail@input.com");
email.textbox.SendKeys(OpenQA.Selenium.Keys.LeftShift + OpenQA.Selenium.Keys.Insert);
you should go to the iframe firstly before you send_keys to input
view the document https://www.selenium.dev/documentation/webdriver/interactions/frames/
driver.switch_to.frame("passport_iframe")
or
# switching to second iframe based on index
iframe = driver.find_elements(By.TAG_NAME,'iframe')[1]
# switch to selected iframe
driver.switch_to.frame(iframe)
after you finish send_keys and want to leave the iframe, you should using this line
# switch back to default content
driver.switch_to.default_content()
Incase the element is with an iFrame so you have to:
Induce WebDriverWait for the desired FrameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired ElementToBeClickable()
.
You can use either of the following Locator Strategies:
Using Name:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.TagName("iframe"));
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("email"))).SendKeys("alexg1380@stackoverflow.com");
Using CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.CssSelector("iframe[frameAttribute='frameAttributeValue']"));
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("[name='email'"))).SendKeys("alexg1380@stackoverflow.com");
Using XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.XPath("//iframe[@frameAttribute='frameAttributeValue']"));
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@name='email']"))).SendKeys("alexg1380@stackoverflow.com");
PS: In the above examples, frameAttribute
and frameAttributeValue
are placeholders.