我需要访问网页上 iframe id =“myFrame”内的元素https://cloud.google.com/products/calculator-legacy
我已经尝试过这个解决方案,但它不起作用:
By frameXpath = By.xpath("//iframe[@id='myFrame']");
new WebDriverWait(driver, Duration.ofSeconds(20))
.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameXpath));
WebElement frame = driver.findElement(frameXpath);
driver.switchTo().frame(frame);
我也尝试过这个解决方案,但它也不起作用:
new WebDriverWait(driver, Duration.ofSeconds(20))
.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(
By.xpath("//devsite-iframe/iframe")));
By frameXpath = By.xpath("//iframe[@id='myFrame']");
new WebDriverWait(driver, Duration.ofSeconds(20))
.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameXpath));
driver.switchTo().frame(driver.findElement(frameXpath));
我有一个例外:“NoSuchElementException:无法定位元素://iframe[@id='myFrame']”
有没有办法访问元素来设置估计条件? 谢谢!
问题是您错过了“myFrame”IFRAME 上方的 IFRAME。
工作代码如下。它切换到两个 IFRAMES,然后将“实例数”字段设置为“1”。
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
...
String url = "https://cloud.google.com/products/calculator-legacy";
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
String numberOfInstances = "1";
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("#cloud-site iframe")));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("myFrame")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("form[name='ComputeEngineForm'] input[ng-model='listingCtrl.computeServer.quantity']"))).sendKeys(numberOfInstances);