Skip to content

Reach for the deep nested element using Selenium WebDriver (Java)

I looked through a lot of topics on Stackoverflow and tried several recommendations but could hardly succeed in resolving my particular case. I’m trying to automate Google Cloud Pricing Calculator using Selenium WebDriver + Java. I need insert the Number of instances (the first input area on the page) using Java code.

My Java code is following:

WebDriver driver = new ChromeDriver();

    new WebDriverWait(driver, 10)
            .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//iframe[contains (@name, 'goog_')]")));

    WebElement document = driver.findElement(By.xpath("//iframe[contains (@name, 'goog_')]"));
    var iframe = document.findElement(By.xpath("//iframe[@id='myFrame']"));
    var input = iframe.findElement(By.xpath("//input[@id='input_66']"));;

But NoSuchElementException is thrown when I launch the code: “no such element: Unable to locate element: {“method”:”xpath”,”selector”:”//iframe[@id=’myFrame’]”}.

Xpath is correct but this element is hidden deep into html tree. How can I reach for the element (Number of instances) in this particular case? Thank you in advance!



You first need to switch to the parent iframe element, then switch into the inner iframe and only after that try accessing the input element.
I see you tried to locate the iframe elements but you do not switch into them.
Try this:

WebDriver driver = new ChromeDriver();

//Find and switch to outer iframe
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//iframe[contains(@src,'product')]")));


WebElement input = driver.findElement(By.xpath("//input[@id='input_66']"));;
User contributions licensed under: CC BY-SA
4 People found this is helpful