简体   繁体   English

Java 中的 Selenium 测试返回错误:连接重置

[英]Selenium test in Java returns the error: Connection reset

I tried some selenium and it worked but stopped in the middle of the crawling process我尝试了一些硒,它起作用了,但在爬行过程中停止了

these are codes:这些是代码:

import java.util.Date;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


public class Kream {
   private static WebDriver driver;
   public static final String WEB_DRIVER_ID = "webdriver.chrome.driver";   // 크롬 드라이버
   public static final String WEB_DRIVER_PATH = "C://chromedriver.exe";
   
   public static void main(String[] args) throws InterruptedException {
      Kream krm = new Kream();
      int rank = 0;   // 순서 주려고 선언
      
      System.setProperty(WEB_DRIVER_ID, WEB_DRIVER_PATH);   // 운영체제 드라이버 설정
      ChromeOptions options = new ChromeOptions();   // 옵션 쓰려고 객체화
      options.addArguments("headless");
      options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
      
      WebDriver driver = new ChromeDriver(options);
      
      try {
         String url = "https://kream.co.kr/search?category_id=34&sort=popular&per_page=40";
         driver.get(url);
         
         List<WebElement> el = driver.findElements(By.className("search_result_item"));

         var stTime = new Date().getTime(); //현재시간
           
         while (new Date().getTime() < stTime + 15000) { //30초 동안 무한스크롤 지속
               Thread.sleep(500); //리소스 초과 방지
               //executeScript: 해당 페이지에 JavaScript 명령을 보내는 거
               ((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight)", el);

            for (WebElement element:el) {
               System.out.println(++rank+". ");
               System.out.print(element.findElement(By.tagName("img")).getAttribute("src"));
               System.out.print("|"+element.findElement(By.className("brand")).getText());         
               System.out.print("|"+element.findElement(By.className("name")).getText());
               System.out.print("|"+element.findElement(By.className("translated_name")).getText());
               System.out.print("|"+element.findElement(By.className("amount")).getText());
               System.out.print("|"+element.findElement(By.className("desc")).getText());
               System.out.print("|"+element.findElement(By.className("express_mark")).getText());
               System.out.println();
               
            }
      
         }
           
      } finally {
         driver.close();
         driver.quit();
      }
   }

}

and return the error that says并返回错误信息

onError


java.net.SocketException: Connection reset
    at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:394)
    at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:426)
    at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:258)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132)
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:357)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:833)

current versions: chromedriver version:103.0.5060.24 chrome version: 103.0.5060.114当前版本:chromedriver 版本:103.0.5060.24 chrome 版本:103.0.5060.114

I already tried to match the both versions so mismatching wouldn't be the reason probably.我已经尝试匹配这两个版本,所以不匹配可能不是原因。

Thank you for the answer in advance.感谢您提前回答。

I made changes to your script and this was working fine as expected, please check the below!我对您的脚本进行了更改,并且按预期工作正常,请检查以下内容! If still you encountered any issues, kindly share complete logs for issue resolution.如果您仍然遇到任何问题,请分享完整的日志以解决问题。

package com.selenium;

import java.util.Date;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Kream {

  private static WebDriver driver;
  public static final String WEB_DRIVER_ID = "webdriver.chrome.driver";   // 크롬 드라이버
  public static final String WEB_DRIVER_PATH = "C://chromedriver.exe";

  public static void main(String[] args) throws InterruptedException {
    Kream krm = new Kream();
    int rank = 0;   // 순서 주려고 선언

    System.setProperty(WEB_DRIVER_ID, WEB_DRIVER_PATH);   // 운영체제 드라이버 설정
    ChromeOptions options = new ChromeOptions();   // 옵션 쓰려고 객체화
    options.addArguments("headless");
    options.setPageLoadStrategy(PageLoadStrategy.NORMAL);

    WebDriver driver = new ChromeDriver();

    try {
      String url = "https://kream.co.kr/search?category_id=34&sort=popular&per_page=40";
      driver.get(url);

      List<WebElement> el = driver.findElements(By.className("search_result_item"));

      var stTime = new Date().getTime(); //현재시간

      while (new Date().getTime() < stTime + 15000) { //30초 동안 무한스크롤 지속
        Thread.sleep(500); //리소스 초과 방지
        //executeScript: 해당 페이지에 JavaScript 명령을 보내는 거
        ((JavascriptExecutor) driver)
            .executeScript("window.scrollTo(0, document.body.scrollHeight)", el);

        for (WebElement element : el) {
          System.out.println(++rank + ". ");
          System.out.print(element.findElement(By.tagName("img")).getAttribute("src"));
          System.out.print("|" + element.findElement(By.className("brand")).getText());
          System.out.print("|" + element.findElement(By.className("name")).getText());
          System.out.print("|" + element.findElement(By.className("translated_name")).getText());
          System.out.print("|" + element.findElement(By.className("amount")).getText());
          System.out.print("|" + element.findElement(By.className("desc")).getText());
//          System.out.print("|" + element.findElement(By.className("express_mark")).getText());
          /*The above tag with class name as express_mark doesn't have any text attribute and hence, this is commented out!*/
          System.out.println();
        }
      }
    } finally {
      driver.close();
      driver.quit();
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM