【问题标题】:Select an option from dropdown box using DOMCrawler in PHP在 PHP 中使用 DOMCrawler 从下拉框中选择一个选项
【发布时间】:2015-03-10 21:37:51
【问题描述】:

我在 PHP 中使用 DOMCrawler。我有下面的 HTML。 我需要能够选择“Text1”选项,然后提交表单。我有以下代码,但我似乎无法让它工作......我做错了什么?

use Goutte\Client;
$client = new Client();
$crawler = $client->request('GET', 'http://myURL');
$form = $crawler->selectButton('Text1')->form();   
$crawler2 = $client->submit($form);

这是 HTML:

<form action="something.php" name="frmOpcion" id="frmOpcion" method="post" enctype="multipart/form-data">

<select name="cmbOpcion" id="cmbOpcion" class="textoCmb">
<option value="a">Text1</option>
<option selected="selected" value="b">Text2</option>
</select>

<input type="image" name="imgOpcion" id="imgOpcion" alt="Send" title="Send" src="goTo.gif">

</form>

【问题讨论】:

    标签: php symfony domcrawler


    【解决方案1】:

    documentation 给出了这个例子:

    // Select an option or a radio
    $form['country']->select('France');
    

    要使示例适应您的情况,请先选择表格。请注意,selectButton() 用于按钮和输入,而不是选择控件:

    $form = $crawler->selectButton('imgOpcion');
    

    接下来,设置select的值:

    $form->select('Text1');
    

    最后,提交表单:

    $client->submit($form)
    

    【讨论】:

    • $form->select() 用于通过选项值而不是选项文本进行选择
    猜你喜欢
    • 1970-01-01
    • 2014-04-15
    • 2017-09-30
    • 1970-01-01
    • 2013-11-12
    • 2017-12-03
    • 2014-04-26
    • 2015-05-17
    • 2018-09-26
    相关资源
    最近更新 更多