[英]WebdriverIO & Java - org.openqa.selenium.NoSuchElementException in IFrame
I'm facing an exception (org.openqa.selenium.NoSuchElementException) when I try to get element "email".当我尝试获取元素“电子邮件”时,我遇到了异常(org.openqa.selenium.NoSuchElementException)。 Since I just started playing with WebDriver I'm probably missing some important concept about those race conditions.
由于我刚刚开始使用 WebDriver,我可能缺少一些关于这些竞争条件的重要概念。
WebElement login = driver.findElement(By.id("login"));
login.click();
WebElement iFrame = driver.findElement(By.id("iFrame"));
driver.switchTo().frame(iFrame);
WebElement email = driver.findElement(By.id("email"));
email.sendKeys(USERNAME);
Few things that I've tried but with no success:我尝试过但没有成功的几件事:
Set an implicitly wait:设置隐式等待:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Create a WebDriverWait:创建一个 WebDriverWait:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement login = driver.findElement(By.id("login"));
login.click();
WebElement iFrame = driver.findElement(By.id("iFrame"));
driver.switchTo().frame(iFrame);
WebElement email = wait.until(presenceOfElementLocated(By.id("email")));
// and WebElement email = wait.until(visibilityOf(By.id("email")));
email.sendKeys(USERNAME);
Create a FluentWait:创建一个 FluentWait:
WebElement login = driver.findElement(By.id("login"));
login.click();
WebElement iFrame = driver.findElement(By.id("iFrame"));
driver.switchTo().frame(iFrame);
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(5))
.ignoring(NoSuchElementException.class);
WebElement email = wait.until(d ->
d.findElement(By.id("email")));
email.sendKeys(USERNAME);
The only way I managed to make it work was using the old and good Thread.sleep() (ugly as well)我设法让它工作的唯一方法是使用旧的和好的 Thread.sleep() (也很丑)
WebElement login = driver.findElement(By.id("login"));
login.click();
WebElement iFrame = driver.findElement(By.id("iFrame"));
driver.switchTo().frame(iFrame);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebElement email = driver.findElement(By.id("email"));
email.sendKeys(USERNAME);
To send a character sequence to the email element as the the desired element is within an <iframe>
so you have to:要将字符序列发送到电子邮件元素,因为所需元素位于
<iframe>
因此您必须:
frameToBeAvailableAndSwitchToIt()
. frameToBeAvailableAndSwitchToIt()
WebDriverWait以获得所需的frameToBeAvailableAndSwitchToIt()
。elementToBeClickable()
.elementToBeClickable()
引入WebDriverWait 。 You can use the following Locator Strategies您可以使用以下定位器策略
driver.findElement(By.id("login")).click(); new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iFrame"))); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("email"))).sendKeys(USERNAME);
Here you can find a relevant discussion on Ways to deal with #document under iframe
在这里你可以找到关于Ways to deal with #document under iframe的相关讨论
It turns out that the code is ok, but Chrome driver 78 linux 64 bits is messed up.原来代码没问题,但是Chrome驱动78 linux 64位就搞砸了。 I've tried with Firefox (geckodriver-0.26.0) and it worked like a charm.
我已经尝试过使用 Firefox (geckodriver-0.26.0),它的效果非常好。
Thanks for the help @DebanjanB感谢@DebanjanB 的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.