java - Selenium 找不到可见的元素
问题描述
这是我尝试查找选定输入的 HTML
我尝试像这样发送key()到这个输入
String xPath = "//*[@id='id_username']";
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xPath))).sendKeys("text");
总是得到这个错误org.openqa.selenium.TimeoutException
。通常,当元素在设定的时间内不可见时,我会收到此错误。
整个 html 中没有 iframe。
可能是哪个原因?
解决方案
您需要考虑以下几点:
- 而不是
String
尝试将xpath定义为By
. sendKeys()
在您调用而不是ExpectedConditions方法visibilityOfElementLocated()使用elementToBeClickable()方法时继续前进。- 由于该元素是
<input>
尝试构建粒度xpath 您的代码块将如下所示:
By xPath = By.xpath("//form[@action='/accounts/register/']/fieldset[@class='fieldset_main']//input[@id='id_username' and @name='username']"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(xPath)).sendKeys("text");
推荐阅读
- apache-spark-sql - 在 spark sql 中获取以分钟或秒为单位的时差
- java - 在 cdi 范围内使用 keycloak 角色授权时出现问题
- sumo - 如何在自定义场景中添加额外的设施?
- tkinter - 如何将焦点设置到 ttk.Entry 小部件?
- python - 使用地图创建数据框导致酸洗错误
- java - 内部存储:为什么 getFilesDir() 给了我错误的文件夹?
- javascript - 地图组件 .getWidth() 不是函数(GeoExt3、MapFish、OpenLayers 4)
- php - PHP Web 应用程序不安全,而我已经使用密码哈希来保护密码
- ruby-on-rails - ruby 重新排序日期数组,字符串部分保留空字符串
- reporting-services - 如何在 SSRS 中进行分组?