java - 如何获取gmail登录表单中包含错误消息的Web元素

标签 java selenium gmail

当我输入错误的密码时,我想获取包含错误消息的 div 元素,以便我可以测试登录是否成功,但我不断收到 org.openqa.selenium.NoSuchElementException

我在网上搜索了一些解决方案,但到目前为止没有一个有效。

我的程序的其余部分工作正常 - 我可以获取电子邮件和密码元素并发送数据。 我正在使用 Selenium 和 java

WebElement msg=driver.findElement(By.id("errormsg_0_Email"));
WebElement child = driver.findElement(By.xpath("//div[contains(@class, 'GQ8Pzc') and text()='Wrong password. Try again or click Forgot password to reset it.']"));

最佳答案

考虑到 gmail 页面/gmail 登录表单会动态生成类和 ID 属性,因此我将通过 normalize-text() 和内部警报文本分析。

请参阅下面的屏幕截图和适当的 XPath 定位器:

//div[normalize-space(text()) = "Couldn't find your Google Account"]

enter image description here

所以在你的情况下,XPath 定位器就像

//*[normalize-space(text()) = "Wrong password. Try again or click Forgot password to reset it."]

关于java - 如何获取gmail登录表单中包含错误消息的Web元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56098115/

相关文章:

java - 处理 id 不断变化的元素

python - AWS Lambda 上的 Headless Chrome Python 3.8 - 意外退出。状态代码为 : 127

java - 无法通过 Selenium 和 WebDriver 定位元素

java - 在运行时移除 FLAG_TRANSLUCENT_STATUS

Java Bytebuffer只能顺序读取?

java - 通知和通知所有显示等待多个线程的奇怪行为

javascript - 使用 Gmail 帐户登录我的应用程序?

java - 如何保存和重用 Eclipse 设置?

python - 使用 OAuth 访问 Gmail 原子提要

php - 使用 GMail 和 phpMailer 发送时为 "Password not accepted from server: 535 Incorrect authentication data"