我怎样才能从 Excel 中逐一读取和运行多个网址,在浏览器中进行操作?

huangapple 未分类评论51阅读模式
标题翻译

How can I read and run multiple URL’s one by one from excel in the browser

问题

请为我提供一些从Excel文件中读取并执行大约300个URL的代码,使用Selenium。

我已经做了类似这样的事情:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.setProperty("webdriver.chrome.driver", "C:\\Users\\Jatin\\Downloads\\chromedriver_win32\\chromedriver.exe");

    WebDriver driver = new ChromeDriver();

    driver.get("http://example.com/");

    List<WebElement> getLinks = driver.findElements(By.tagName("a"));  
    // 获取网站上的URL列表
    System.out.println(getLinks.size());

    // 从Excel中获取数据
    try {
        File excel = new File("C:\\Users\\Jatin\\Documents\\Output.xlsx");
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook book = new XSSFWorkbook(fis);
        XSSFSheet sheet = book.getSheetAt(0);

        Iterator<Row> itr = sheet.iterator();

        // 在Java中迭代Excel文件
        while (itr.hasNext()) {
            Row row = itr.next();

            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {

                Cell cell = cellIterator.next();

                switch (cell.getCellType()) {
                    case STRING:
                        System.out.print(cell.getStringCellValue() + "\t");
                        break;
                    case NUMERIC:
                        System.out.print(cell.getNumericCellValue() + "\t");
                        break;
                    case BOOLEAN:
                        System.out.print(cell.getBooleanCellValue() + "\t");
                        break;
                    default:
                }
            }
            System.out.println("");
        }
    } catch (Exception e) {
        // 处理异常
        e.printStackTrace();
    }
}
英文翻译

Please suggest me some code for the reading and executing approx 300 URLs from excel file in selenium.

I have done something like this:

public static void main(String[] args) {
		// TODO Auto-generated method stub

		System.setProperty(&quot;webdriver.chrome.driver&quot;, &quot;C:\\Users\\Jatin\\Downloads\\chromedriver_win32\\chromedriver.exe&quot;);

		 WebDriver driver = new ChromeDriver();
		 
		 driver.get(&quot;http://example.com/&quot;);
		 		 
		 List&lt;WebElement&gt; getLinks = driver.findElements(By.tagName(&quot;a&quot;));  
         // to get the list of urls from the website 
		 System.out.println(getLinks.size());
		

         //fetch data from excel
		 try {
		        File excel = new File(&quot;C:\\Users\\Jatin\\Documents\\Output.xlsx&quot;);
		        FileInputStream fis = new FileInputStream(excel);
		        XSSFWorkbook book = new XSSFWorkbook(fis);
		        XSSFSheet sheet = book.getSheetAt(0);

		        Iterator&lt;Row&gt; itr = sheet.iterator();

		        // Iterating over Excel file in Java
		        while (itr.hasNext()) {
		            Row row = itr.next();

		            Iterator&lt;Cell&gt; cellIterator = row.cellIterator();
		            while (cellIterator.hasNext()) {

		                Cell cell = cellIterator.next();

		                switch (cell.getCellType()) {
		                case STRING:
		                    System.out.print(cell.getStringCellValue() + &quot;\t&quot;);
		                    break;
		                case NUMERIC:
		                    System.out.print(cell.getNumericCellValue() + &quot;\t&quot;);
		                    break;
		                case BOOLEAN:
		                    System.out.print(cell.getBooleanCellValue() + &quot;\t&quot;);
		                    break;
		                default:


		                }
		            }
		            System.out.println(&quot;&quot;);}
		        }catch (Exception e) {
		            // TODO: handle exception
		            e.printStackTrace();
		        }
	}

huangapple
  • 本文由 发表于 2020年5月30日 16:37:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/62099926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定