硒在检查元素之前无法找到元素

时间:2020-02-19 11:11:02

标签: html selenium

我在这里遇到了类似的问题: Selenium - can't find visible element until HTML is inspected? 但是解决方案表明那里没有帮助我。

有一个网站具有以下保管箱(检查页面时可以看到它们): enter image description here

当我尝试切换到所需的帧时未引发错误,id = UIFormControl-5未找到任何元素。

因此,在检查了保管箱(id = UIFormControl-5)并没有进行任何其他更改之后,元素就在那里了,就好像仅仅查看网页源代码的行为改变了结果一样。这里可能会发生什么?

我正在使用Java硒代码

            driver.switchTo().frame(driver.findElement(By.cssSelector("iframe.flex-grow-1")));
            wait.until(ExpectedConditions.presenceOfElementLocated(By.id("UIFormControl-5")));
            driver.findElement(By.id("UIFormControl-5")).click();

1 个答案:

答案 0 :(得分:0)

得出WebDriverWait()并等待frameToBeAvailableAndSwitchToIt()

引出WebDriverWait()并等待elementToBeClickable()然后单击。

new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.flex-grow-1")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("UIFormControl-5"))).click();