如何在外部站点中自动执行提交操作

时间:2014-02-05 09:35:00

标签: java javascript php html

这是我的问题:

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

我希望有人帮助我:) 谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

您可以尝试通过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));

答案 1 :(得分:0)

或者,您可以使用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();

}
}