繁体   English   中英

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

[英]How to perform a submit action automatically in an external site

那是我的问题:

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

希望有人能帮到我:)谢谢您的帮助!

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

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

这里拍摄:

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();

}
}

暂无
暂无

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

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