简体   繁体   English

如何使用Selenium Webdriver在新网页中查找元素?

[英]How to find an element in new Webpage using selenium webdriver?

I have a form. 我有一个表格。 I automated the task of filling up the form and clicking the submit button using Selenium WebDriver. 我使用Selenium WebDriver自动完成了填写表单并单击“提交”按钮的任务。

Now in the next page where the URL has changed, there is a confirmation message "Form Submitted Successfully". 现在,在URL更改的下一页中,将显示一条确认消息“成功提交表单”。 How do I verify this element in the new URL? 如何在新网址中验证此元素?

Thanks in advance! 提前致谢!

Try to use JUnit assertion to compare expected string with actual the one you getting on the page, this should work : 尝试使用JUnit断言将期望的字符串与实际在页面上获得的字符串进行比较,这应该可以:

String actual = driver.findElement(By.xpath("your_locator").getText();
Assert.assertEquals(actual, expected);

As per my knowledge, even if the objects you are working on are on different page(url) it does not matter, you just need to have your locator correctly written. 据我所知,即使您正在处理的对象位于不同的页面(url)上也没关系,您只需要正确编写定位符即可。

Using getText() method you could get text on the page. 使用getText()方法可以在页面上获取文本。 Verify whether your locator is pointing to correct text using xpath validaters or other means. 使用xpath验证程序或其他方式验证定位器是否指向正确的文本。

Once you have your locator handy apply getText() method on it and get the text. 一旦有了定位器,就可以在其上应用getText()方法并获取文本。

Once you have text, just verify it or print it to see if it is the same. 收到文字后,只需对其进行验证或打印,以查看其是否相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM