我需要帮助创建一个函数,该函数将从Java(Selenium)中的CSV文件读取(打开新浏览器选项卡)url

我需要帮助创建一个函数,该函数将从Java(Selenium)中的CSV文件读取(打开新浏览器选项卡)url,java,selenium,Java,Selenium,我正在尝试自己学习java和selenium,并创建一个机器人来扫描工作/职业页面中的特定字符串(工作名称,例如QA、开发人员…) 我正在尝试使用selenium创建JAVA代码,它将从CSV文件中读取URL链接并打开一个新选项卡 主要目标是在CSV中添加几个url,并在指定url中断言/定位特定字符串。例如:每个url中是否有“职业”链接,此特定url的测试将通过 创建了一个selenium项目 创建新的chromeDriver 创建了由3列(ID、公司名称、URL)构建的CSV,并将其添加到

我正在尝试自己学习java和selenium,并创建一个机器人来扫描工作/职业页面中的特定字符串(工作名称,例如QA、开发人员…)

我正在尝试使用selenium创建JAVA代码,它将从CSV文件中读取URL链接并打开一个新选项卡

主要目标是在CSV中添加几个url,并在指定url中断言/定位特定字符串。例如:每个url中是否有“职业”链接,此特定url的测试将通过

  • 创建了一个selenium项目
  • 创建新的chromeDriver
  • 创建了由3列(ID、公司名称、URL)构建的CSV,并将其添加到项目中
  • import org.openqa.selenium.chrome.ChromeDriver;
    导入java.io.File;
    导入java.io.FileNotFoundException;
    导入java.util.Scanner;
    公共类URL搜索{
    公共静态void main(字符串[]args){
    ChromeDriver驱动程序=新的ChromeDriver();
    driver.manage().window().maximize();
    字符串fileName=“JobURLList.csv”;
    File File=新文件(fileName);//从文件中读取
    试一试{
    扫描仪输入流=新扫描仪(文件);
    while(inputStream.hasNext()){
    字符串数据=inputStream.next();
    系统输出打印项次(数据);
    }
    inputStream.close();
    }catch(filenotfounde异常){
    e、 printStackTrace();
    }
    }
    }
    
  • CSV中的第一行-标题:id、名称、url
  • 从第二行读取url-例如。“
  • 打开browsertab并开始浏览url列表(从CSV)
  • 在每个url中找到硬编码字符串(例如“开发者”、“qa”)
  • 如果找到这样一个字符串,请在控制台中写入测试结果为阳性的url(在某个url中找到了这样一个字符串)
  • 如果没有找到这样的字符串,请跳到下一个url

  • 要打开新选项卡,请执行以下操作(假定“驱动程序”对象是您的WebDriver):

    ((JavascriptExecutor)驱动程序).executeScript(“window.open('about:blank','u blank');”;
    设置tab_handles=driver.getWindowHandles();
    int number_of_tabs=tab_handles.size();
    int new_tab_index=_tabs的数量-1;
    driver.switch to().window(tab_handles.toArray()[new_tab_index].toString());
    
    然后,您可以创建一个函数,该函数包含一个键/值对列表,其中包含要搜索和循环的URL和术语。是否要为此使用hashmap,或者使用类(id/name/URL)的ArrayList?查找文本的代码如下所示(假设您已定义了一个“Pass”到boolean的变量):

    driver.get([var代表URL]);
    //驱动程序将等待pageready状态,因此您可以
    //不需要下面使用的webdriver等待。具体取决于
    //如果页面在pagereadystate之后填充数据,则启用
    字符串xpather=“//*[contains(text(),”+[String var用于搜索文本]+”)];
    尝试
    {
    等待=新的WebDriverWait(驱动程序,10);
    List元素=wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(xpather));
    这个.Pass=false;
    if(element.size()>0)
    {
    这个.Pass=true;
    }
    }
    捕获(例外情况除外)
    {
    这个.Pass=false;
    System.out.println(“异常查找文本:+ex.toString());
    }
    

    然后使用if逻辑(this.Pass==true或false)

    打开新选项卡,执行如下操作(假设“driver”对象是您的WebDriver):

    ((JavascriptExecutor)驱动程序).executeScript(“window.open('about:blank','u blank');”;
    设置tab_handles=driver.getWindowHandles();
    int number_of_tabs=tab_handles.size();
    int new_tab_index=_tabs的数量-1;
    driver.switch to().window(tab_handles.toArray()[new_tab_index].toString());
    
    然后,您可以创建一个函数,该函数包含一个键/值对列表,其中包含要搜索和循环的URL和术语。是否要为此使用hashmap,或者使用类(id/name/URL)的ArrayList?查找文本的代码如下所示(假设您已定义了一个“Pass”到boolean的变量):

    driver.get([var代表URL]);
    //驱动程序将等待pageready状态,因此您可以
    //不需要下面使用的webdriver等待。具体取决于
    //如果页面在pagereadystate之后填充数据,则启用
    字符串xpather=“//*[contains(text(),”+[String var用于搜索文本]+”)];
    尝试
    {
    等待=新的WebDriverWait(驱动程序,10);
    List元素=wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(xpather));
    这个.Pass=false;
    if(element.size()>0)
    {
    这个.Pass=true;
    }
    }
    捕获(例外情况除外)
    {
    这个.Pass=false;
    System.out.println(“异常查找文本:+ex.toString());
    }
    

    那么if的逻辑(this.Pass==true或false)

    我似乎无法添加我拥有的完整代码我似乎无法添加我拥有的完整代码
      ((JavascriptExecutor)driver).executeScript("window.open('about:blank', '_blank');"); 
    
            Set<String> tab_handles = driver.getWindowHandles();
            int number_of_tabs = tab_handles.size();
            int new_tab_index = number_of_tabs-1;
            driver.switchTo().window(tab_handles.toArray()[new_tab_index].toString());
    
       driver.get([var for URL]);
       //driver will wait for pageready state, so you may
       // not need the webdriver wait used below.  Depends
       // on if the page populates data after pagereadystate
          String xpather = "//*[contains(text(), '" + [string var for text to search for] + "')]";
            try
                {
            wait = new WebDriverWait(driver, 10);
                List<WebElement> element =  wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(xpather)));   
            this.Pass = false;
            if (element.size() > 0)
            {
                this.Pass = true;
    
            }
    
                }
                catch (Exception ex)
                {
                    this.Pass = false;
                    System.out.println ("Exception finding text: " + ex.toString());
                }