当我输入错误的密码时,我想获取包含错误消息的 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"]
所以在你的情况下,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/