java - 循环遍历文件并使用正则表达式查找文本

标签 java regex selenium

我正在使用 Selenium 来测试网页,并希望采用一种更简单的方法来更新测试用例(对于问题来说并不重要)。

我现在用这个循环遍历各行:

      driver.get("http://vg.no"); //open the web page
  try {
        BufferedReader reader = new BufferedReader(new FileReader("//Users//file.txt"));
        try {
            String line = null;
            while ((line = reader.readLine()) != null) {
                driver.findElement(By.cssSelector(line)).click();; //find and click on the data specified in every line in the document
            }
        } finally {
            reader.close();
        }
    } catch (IOException ioe) {
        System.err.println("oops " + ioe.getMessage());
    }

现在的文本文件内容示例:

a[href*='//nyheter//meninger//']
img[class*='logo-red']
img[class*='article-image']

我想将其重建为一个基于正则表达式启动不同命令的解决方案。 我尝试让它以这种方式工作:

vg.no //this will start driver.get("vg.no")
  img[class*='logo-red'] //this will start driver.findElement(By.cssSelector("img[class*='logo-red']")).click()
  img[class*='article-image']
ItAvisen.no 
  img[class*='article-image']
  img[class*='article-image']

有没有办法使用正则表达式根据文本文件中的内容启动代码的不同部分,并将文本文件中的部分内容用作变量?

<小时/>

在cvester反馈后,它是这样工作的:

查找 img[class*='logo-red'] 的匹配

                String regexp = "img\\[class\\*=\\'*\\'(.*)\\]";
                boolean match = line.matches(regexp);

最佳答案

它仍然是基于线路的吗? 在这种情况下,您可以逐行读取并针对您识别的每种情况使用 String.matches(String regex)

如果您可以指定更具体的信息,我也许可以为您提供更好的解决方案。

关于java - 循环遍历文件并使用正则表达式查找文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31315228/

相关文章:

java - Ubuntu下构建libvirt-java

java - 将表格边框设置为粗

java - 插入性能调整

regex - 尝试从 Postman : TypeError: postman. getResponseHeader 中的 post 调用获取 session 值不是函数

javascript - 正则表达式替换特殊字符在 JS 和 Python 中给出不同的结果

java - kafka 中的每条消息使用不同的 key

javascript - 正则表达式中的感叹号

java - selenium 中 ApacheHttpClient 类的替代方案是什么

Python Selenium : How do I click a link in a drop down menu with css selector?

java - 如何从列表中删除空项目?