1

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");
alexg1380
  • 23
  • 2

3 Answers3

0

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);
Oscar Sun
  • 1,427
  • 2
  • 8
  • 13
0

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()
Jiu_Zou
  • 463
  • 1
  • 4
0

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352