处理Web爬虫中的AJAX块或创建手动输入 [英] Deal with AJAX block in web-crawler or create manually inputs

查看:161
本文介绍了处理Web爬虫中的AJAX块或创建手动输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于Alvin Bunk文章链接到文章我想创建一个web-cralwer登录网站,然后提交表单。



我的问题是,该网站有一个Ajax块,它在点击和空链接后生成一些我需要填写的输入,所以我需要点击那个空链接或手动插入输入。



我通过很多方式改变了下面的代码,试图让它工作,但是在访问函数中,我被卡住了



我得到未捕获的错误:调用到一个成员函数visit()null

 <?php 
require'vendor / autoload .PHP';

trait MinkSetup
{
private $ minkBaseUrl;
private $ minkSession;
$ b $ **
* @before
* /
public function setupMinkSession()
{
$ this-> minkBaseUrl =' https://www.url.com;
$ driver = new \Behat\Mink\Driver\Selenium2Driver('firefox');
$ this-> minkSession = new \Behat\Mink\Session($ driver);
$ this-> minkSession-> start();

$ b $ public function getCurrentPage()
{
return $ this-> minkSession-> getPage();
}

public function getCurrentPageContent()
{
return $ this-> getCurrentPage() - > getContent();
}

公共函数访问($ url)
{
echo $ url;
$ this-> minkSession->造访($ url);


public function login($ user,$ pass){
$ this-> minkSession-> visit('complete url');
$ page = $ this-> getCurrentPage();
echo $ page;
$ page-> fillField('email',$ user); // 输入用户名。
$ page-> fillField('password',$ pass); // 输入密码。
$ page-> pressButton('Login');

$ content = $ this-> getCurrentPageContent();
$ this-> assertContains('logout',$ content);

$ b / **
* @afterClass
* /
public function logout(){
$ page = $ this-> ; getCurrentPage();
$ page-> clickLink('logout');
}
}
使用PHPUnit\Framework\TestCase;


类MinkPetitionTest扩展TestCase
{
使用MinkSetup;

public function testSubmitPage(){
$ this-> login('user','pw'); //首先登录。

$ this-> visit('full url');
$ page = $ this-> getCurrentPage(); //获取页面。
echo $ page;
$ page-> fillField('form_ban_id','1234');
$ page-> pressButton('form_find_student');

$ content = $ this-> getCurrentPageContent(); //获取页面内容。
$ this-> assertContains('< u>对于某用户学生ID:1234存在不存在请求',$ content);
}
}



$ client = new MinkPetitionTest(); //试图让某些东西起作用
$ client-> testSubmitPage(); // same here


解决方案

您需要更改测试类如果您使用最新的phpunit,则可以这样做:

  ... 
使用PHPUnit\Framework\TestCase;

class MinkPetitionTest extends TestCase
{
...






编辑#2


您可以先试试看看结果吗?

另外,您的特质文件不正确。它应该是:

  trait MinkSetup 
{
private $ minkBaseUrl;
...

public function visit($ url)
{
$ this-> minkSession-> visit($ this-> minkBaseUrl。$ URL);
}
...

尝试

Based on Alvin Bunk article link to article I want to create a web-cralwer that logins in a website then submits a form.

My problem is that on that website there is an Ajax block that generates after clicking and empty link few inputs that I need to fill so I need to click that empty link somehow or to insert the inputs manually .

I changed the code below in a lot of ways to try to make it work but on the visit function I got stuck

I get Uncaught Error: Call to a member function visit() on null

<?php
 require 'vendor/autoload.php';

trait MinkSetup
{
    private $minkBaseUrl;
    private $minkSession;

    /**
     * @before
     */
    public function setupMinkSession()
    {
        $this->minkBaseUrl = 'https://www.url.com';
        $driver = new \Behat\Mink\Driver\Selenium2Driver('firefox');
        $this->minkSession = new \Behat\Mink\Session($driver);
        $this->minkSession->start();
    }

    public function getCurrentPage()
    {
        return $this->minkSession->getPage();
    }

    public function getCurrentPageContent()
    {
        return $this->getCurrentPage()->getContent();
    }

    public function visit($url)
    {
         echo $url;
        $this->minkSession->visit($url);
    }

    public function login($user, $pass){
        $this->minkSession->visit('complete url');
        $page = $this->getCurrentPage();
        echo $page;
        $page->fillField('email', $user); // Enter username.
        $page->fillField('password', $pass); // Enter password.
        $page->pressButton('Login');

        $content = $this->getCurrentPageContent();
        $this->assertContains('logout', $content);
    }

    /**
     * @afterClass
     */
    public function logout(){
        $page = $this->getCurrentPage();
        $page->clickLink('logout');
    }
}
use PHPUnit\Framework\TestCase;


class MinkPetitionTest extends  TestCase
{
    use MinkSetup;

    public function testSubmitPage(){
        $this->login('user', 'pw'); // Login first.

        $this->visit('full url');
        $page = $this->getCurrentPage(); // Get the page.
        echo $page;
        $page->fillField('form_ban_id', '1234');
        $page->pressButton('form_find_student');

        $content = $this->getCurrentPageContent();   // Get page content.
        $this->assertContains('<u>No Petitions</u> exist for Some User Student ID: 1234', $content);
    }
}



$client = new MinkPetitionTest(); //tried to get something to work
$client->testSubmitPage(); //same here

解决方案

You need to change your test class like so if you are using the latest phpunit:

...
use PHPUnit\Framework\TestCase;

class MinkPetitionTest extends TestCase
{
   ...

Can you try that first and see the result?


EDIT #2

Also, your trait file is incorrect. it should be:

trait MinkSetup
{
    private $minkBaseUrl;
    ...

    public function visit($url)
    {
        $this->minkSession->visit($this->minkBaseUrl . $url);
    }
    ...

Try that

这篇关于处理Web爬虫中的AJAX块或创建手动输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆