如何在Selenium Java中使元素可见

4
当我尝试执行以下代码时,它会给我一个异常: org.openqa.selenium.ElementNotVisibleException: element not visible
 WebElement elem = newDriver.findElement(By.name("loginId"));
    elem.get(0).clear();
    elem.get(0).sendKeys("asd");

尽管元素存在,但我仍然无法访问下面帐户标签下的输入框。

更多细节请参见下面的图像。

enter image description here

我已经使用了Actions标签和JavascriptExecutor,但它仍然给出元素不可见的异常。

有什么建议吗?


你可以尝试这个链接: https://dev59.com/FnjZa4cB1Zd3GeqPensM - Mr.Bob
@Kapil123,谢谢你的建议。 - Swagat
1个回答

1

感兴趣的元素位于一个iframe内:

enter image description here

所以,在此之前,您需要切换到iframe:
    WebElement iframe= driver.findElement(By.id("alibaba-login-box"));
    driver.switchTo().frame(iframe);

如果你想要“退出”iframe:
driver.switchTo().defaultContent();

整个代码:

    WebElement iframe= driver.findElement(By.id("alibaba-login-box"));
    driver.switchTo().frame(iframe);
    WebElement elem = driver.findElement(By.id("fm-login-id"));
    elem.clear();
    elem.sendKeys("asd");

    //when you want to return to the defaultContent
    driver.switchTo().defaultContent();

非常感谢,它起作用了。 我为这个问题苦苦挣扎了5个小时,再次感谢你的帮助。 - Swagat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接