[英]How to verify links and click on them in Selenium webdriver Java
I am new in selenium. 我是硒新手。 I have similar links on the page with similar xpaths (let's say links 1 to 30).
我在页面上有具有相似xpath的相似链接(比方说链接1到30)。 I need to verify that all of them are present on the page (I want selenium to print something like this "element 1 is present" for each element) and all of them are clickable(new page will opens).
我需要验证所有这些元素是否都在页面上(我希望硒为每个元素打印类似“此元素存在1”之类的东西)并且它们都是可单击的(将打开新页面)。 So far I have this code to click on all links one by one and print their titles:
到目前为止,我已经有了这段代码来单击所有链接,并打印其标题:
driver.get("http://anysite.com");
String part1=".//*[@id='footer']/div/div[1]/div[2]/ul/li[";
String part2="]/a";
for (int i=1;i<=30;i++){
String text = driver.findElement(By.xpath(part1+i+part2)).getText();
System.out.println(text);
driver.findElement(By.xpath(part1+i+part2)).click();
System.out.println(driver.getTitle());
driver.get("http://anysite.com");
Also I have this code to verify presence of each individual link separately (if I have 30 links I have to duplicate this code 30 times): 另外,我有以下代码可以分别验证每个单独的链接的存在(如果我有30个链接,则必须将此代码重复30次):
if(driver.findElements(By.xpath(".//*[@id='footer']/div/div[1]/div[2]/ul/li[1]/a")).size() != 0){
System.out.println("Element Link 1 is Present");
}else{
System.out.println("Element Link 1 is Absent");
}
So far I have two test cases, is there is any way to combine this two pieces together in one nice piece? 到目前为止,我有两个测试用例,是否有任何方法可以将这两部分组合成一个不错的部分? Thank you!
谢谢!
In String 1 you are taking xpath as: 在字符串1中,您将xpath用作:
String part1=".//*[@id='footer']/div/div[1]/div[2]/ul/li[";
while it should be: 而应该是:
String part1="//*[@id='footer']/div/div[1]/div[2]/ul/li[";
You need to remove dot "." 您需要删除点“。” from your xpath.
从您的xpath。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.