我需要访问网页https://cloud.google.com/products/calculator-legacy上iframe id ="myFrame"内部的元素

我try 过这个解决方案,但不起作用:

By frameXpath = By.xpath("//iframe[@id='myFrame']");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameXpath));

WebElement frame = driver.findElement(frameXpath);
driver.switchTo().frame(frame);

我也try 过这个解决方案,但它也不起作用:

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//devsite-iframe/iframe")));

By frameXpath = By.xpath("//iframe[@id='myFrame']");

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameXpath));

driver.switchTo().frame(driver.findElement(frameXpath));

我有一个例外:

"NoSuchElementException: Unable to locate element: //iframe[@id='myFrame']"

是否有一种方法可以访问元素来设置估计的条件? 谢谢!

推荐答案

有几个问题

  1. 您错过了"myFrame"IFRAME上方的IFRAME.

<iframe SRC="https://cloud.google.com/frame/products/calculator-legacy/index_d6a98ba38837346d20babc06ff2153b68c2990fa24322fe52c5f83ec3a78c6a0.frame"allow="clipboard-writer https://cloud-dot-devsite-v2-prod.appspot.com"allowfullscreen=""名称="goog_1486242109">

  1. 您正在使用.frameToBeAvailableAndSwitchToIt(),但随后try 再次切换到IFRAME..frameToBeAvailableAndSwitchToIt()等待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);

Java相关问答推荐

Java中Stream(java.util.stream)和linkedList数据 struct 之间是什么关系?

为什么我的画布没有显示在PFA应用程序中?

更新我们的一个文物后出现了严重的符号引用错误

AlarmManager没有在正确的时间发送alert

@ IdClass with @ Inheritance(策略= InheritanceType. SINGLE_TABLE)

需要一个找不到的jakarta.sistence.EntityManager类型的Bean

使用Mockito进行的Junit测试失败

如何使用值中包含与号的查询参数创建一个java.net.URI

Jenv-相同的Java版本,但带有前缀

用OSQL创建索引

匹配一组字符或另一组字符

Groovy/Java:匹配带引号的命令选项

是否为计划任务补偿系统睡眠?

Java堆中的许多java.time.ZoneRegion实例.ZoneId实例不应该被缓存吗?

当b是一个字节并且在Java中值为-1时,为什么b>;>;>;1总是等于-1?

IntelliJ IDEA依赖项工具窗口丢失

在缺少字段时使用Jackson With Options生成Optional.Empty()

如何在JSP中从select中获取值并将其放入另一个select

当我try 返回可选时,Mock无法正常工作

将天数添加到ZonedDateTime不会更改时间