Find Elements Inside Forms and Iframe Using Java and Selenium Webdriver

Find elements inside forms and iframe using Java and Selenium WebDriver

Before you try searching for the elements within the iframe you will have to switch Selenium focus to the iframe.

Try this before searching for the elements within the iframe:

driver.switchTo().frame(driver.findElement(By.name("iFrameTitle")));

Unable to locate element within an iframe through Selenium

You need to switch to the frame to write text in the textbox, try to check syntax once as I have less in good in Python

framLogin= driver.find_element_by_id("membeeLoginIF")
driver.switch_to.frame(framLogin)
EmailTxt = driver.find_element_by_name("txtUserName");
EmailTxt.send_Keys("Test@gmail.com");

Same in Java

WebElement framLogin= driver.findElement(By.id("membeeLoginIF"));
driver.switchTo().frame(framLogin);
WebElement EmailTxt = driver.findElement(By.name("txtUserName"));
EmailTxt.sendKeys("Test@gmail.com");

How to locate an element inside iFrame using Selenium and Python

As the the desired element is within an <iframe> so to invoke click() on the element you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following Locator Strategies::

    • Using XPATH:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@sandbox='allow-scripts allow-same-origin' and contains(@class, 'credit-card-iframe')]")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='creditCardNumber' and @data-shortname='cc']"))).send_keys("35663565444")
    • Note : You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC

Reference

You can find a relevant discussion in:

  • Ways to deal with #document under iframe

Access Element in Iframe with Selenium

Ok guys this is the Solution. i take the method with the Webelement

driver.switchTo().frame(driver.findElement(By.name("1")));

Selenium - Unable to find input elements inside iframe using SwitchTo()

wait=WebDriverWait(driver,10) 
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"#tab1")))

You are using Java use Python instead and waits.

driver.SwitchTo().Frame(driver.FindElement(By.id("tab1")));

Imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Not able to find an element in iframe using Xpath and Selenium

To click() on the element to search, as the the desired element is within a <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 cssSelector:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe_cssSelector")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("td.gsc-search-button > button.gsc-search-button.gsc-search-button-v2"))).click();
    • Using xpath:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("iframe_xpath")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[@class='gsc-search-button']/button[@class='gsc-search-button gsc-search-button-v2']"))).click();


Reference

You can find a couple of relevant discussions in:

  • Ways to deal with #document under iframe
  • Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
  • How to click within the username field within ProtonMail signup page using Selenium and Java


Related Topics



Leave a reply



Submit