如何验证网页上是否存在从文件中读取的链接?

时间:2013-08-15 17:19:54

标签: file-io hyperlink click selenium-webdriver

我是自动化新手。我必须编写如下代码

我必须从一个文件中读取大约10个url并将其存储到一个哈希表中然后我需要从哈希表中逐个读取url并且在遍历此url时我还需要再读取一个包含3个url的文件并搜索它们在网页上。如果存在需要点击该链接

我已编写以下代码,但我没有得到检查网页上是否存在文件链接的逻辑... 请检查我的代码并帮助我解决/改进它。

Main test script 
package com.samaritan.automation;

import java.util.Hashtable;
import java.util.Set;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FirstScript {

    WebDriver driver = new FirefoxDriver();     
    String data;
    CommonControllers commonControll = null;
    Hashtable<String, String> recruiters = null;


    @Test
    public void script() throws Exception {  
        CommonControllers commonControll = new CommonControllers();
        recruiters = new Hashtable<String,String>();

        recruiters = commonControll.readDataFromFile("D:/eRecruiters/_Recruiters.properties");

        Set<String> keys = recruiters.keySet();

        for(String key: keys){

        /**HERE I NEED TO WRITE THE FUNCTION TO VERIFY WHETHER THE LINK READ FROM SECOND FILE IS PRESENT ON WEBPAGE OR NOT**/
        }
   }    

}

和从文件读入哈希表的功能

public Hashtable<String, String> readDataFromFile(String fileName) {
            try {
                FileReader fr = new FileReader(fileName);
                BufferedReader br = new BufferedReader(fr);
                String strLine = null;
                String []prop = null;
                while((strLine = br.readLine()) != null) {
                    prop = strLine.split("\t");
                    recruiters.put(prop[0], prop[1]);
                }
                br.close();
                fr.close();

            }catch(Exception exception) {
                System.out.println("Unable to read data from recruiter file: " + exception.getMessage());
            }
            return recruiters;
        }

请看看!感谢

1 个答案:

答案 0 :(得分:0)

Priya ......您可以使用

 if(isElementPresent(By.linkText(LinkTextFoundFromFile))){
      //code when link text present there
 }else {
      //code for not finding the link
 }

现在,您可以使用ByBy.xpath之类的任何By.id对象推广以下方法。

 private boolean isElementPresent(By by) {
     try {
       driver.findElement(by);
       return true;
     } catch (NoSuchElementException e) {
       return false;
     }
   }