【问题标题】:How to perform a submit action automatically in an external site如何在外部站点中自动执行提交操作
【发布时间】:2026-01-01 05:45:01
【问题描述】:

这是我的问题:

我每天访问一个网站(当然需要登录)并上传相同的图像。 我想知道是否有办法使这个过程自动化。

我希望有人可以帮助我:) 感谢您的帮助!

【问题讨论】:

    标签: java javascript php html


    【解决方案1】:

    或者,您可以使用Selenium(通过他们自己的Java Client):

    取自here

    import junit.framework.Assert;
    import junit.framework.TestCase;
    import org.openqa.selenium.*;
    import org.openqa.selenium.remote.*;
    import java.net.URL;
    import java.util.concurrent.TimeUnit;
        public class TestingUploadSe2Sauce extends TestCase {
        private RemoteWebDriver driver;
        public void setUp() throws Exception {
                DesiredCapabilities capabillities = DesiredCapabilities.firefox();
                capabillities.setCapability("version", "7");
                capabillities.setCapability("platform", Platform.XP);
                capabillities.setCapability("selenium-version", "2.18.0");
                capabillities.setCapability("name", "Remote File Upload using Selenium 2's FileDetectors");
            driver = new RemoteWebDriver(
                new URL("http://<username>:<api-key>@ondemand.saucelabs.com:80/wd/hub"),
            capabillities);
            driver.setFileDetector(new LocalFileDetector());
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
    
    public void testSauce() throws Exception {
            driver.get("http://sso.dev.saucelabs.com/test/guinea-file-upload");
            WebElement upload = driver.findElement(By.id("myfile"));
            upload.sendKeys("/Users/sso/the/local/path/to/darkbulb.jpg");
            driver.findElement(By.id("submit")).click();
            driver.findElement(By.tagName("img"));
            Assert.assertEquals("darkbulb.jpg (image/jpeg)", driver.findElement(By.tagName("p")).getText());
    }
    public void tearDown() throws Exception {
          driver.quit();
    
    }
    }
    

    【讨论】:

      【解决方案2】:

      您可以尝试通过 curl 提交文件。

       $file_name_with_full_path = realpath('./sample.jpeg');
       $post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL,$target_url);
       curl_setopt($ch, CURLOPT_POST,1);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
       $result=curl_exec ($ch);
       curl_close ($ch);
      

      要登录到特定网站并保留 cookie,您可以执行类似以下的功能:

      public static function curlResult ($url, $getHeaders = false, $data) {
      
          $options = array(
              CURLOPT_RETURNTRANSFER => true,     // return web page
              CURLOPT_HEADER         => false,    // return headers
              CURLOPT_FOLLOWLOCATION => true,     // follow redirects
              CURLOPT_ENCODING       => "",       // handle all encodings
              CURLOPT_USERAGENT      => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1",
              CURLOPT_AUTOREFERER    => true,     // set referer on redirect
              CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
              CURLOPT_TIMEOUT        => 120,      // timeout on response
              CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
              CURLOPT_COOKIESESSION  => true,
              CURLOPT_COOKIEJAR      => dirname(__FILE__) . '/cookie.txt',
              CURLOPT_COOKIEFILE     => dirname(__FILE__) . '/cookie.txt'
          );
      
          $ch = curl_init( $url );
          curl_setopt_array( $ch, $options );
      
          if ($data != false) {
             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
             curl_setopt($ch, CURLOPT_POST, $data?1:0);
          }
      
          $content = curl_exec( $ch );
          $header  = curl_getinfo( $ch );
          curl_close( $ch );
      
          if ($getHeaders) {
              return $header;
          }
      
          return $content;
      }
      

      然后使用类似的函数:

      // Login 
      curlResult('urltologin', false, array('username' => 'test', 'password' => 'demo');
      // Upload
      curlResult('urltoupload', false, array(file_contents'=>'@'.$file_name_with_full_path));
      

      【讨论】:

        最近更新 更多