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(); driver.get("https://cloud.google.com/products/calculator"); 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']")); input.click(); input.sendKeys("4");
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!
Advertisement
Answer
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(); driver.get("https://cloud.google.com/products/calculator"); //Find and switch to outer iframe new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//iframe[contains(@src,'product')]"))); driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'product')]"))); driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='myFrame']"))); WebElement input = driver.findElement(By.xpath("//input[@id='input_66']")); input.click(); input.sendKeys("4");