本文介绍了CasperJS动态选择列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助

我正在从这个网站上抓取数据,该网站的表单包含三个相互链接的选择列表,即如果第一个列表中的任何选项选择列表被选中,此函数称为 onchange = Javascript:submitForm2(); ,并填充第二个选择列表。

I am scraping data from this website which has a form that contains three selectlists interconnected to each other that is if the any option from the from the first select list is selected this function is called onchange="Javascript:submitForm2(); and the second selectlist is populated.

随后,如果从第二个选择列表中选择了一个选项,则相同的js函数称为 onchange = Javascript:submitForm2(); ,最后有两个此表单的提交按钮

And subsequently if an option from the second selectlist is selected the same js function is called onchange="Javascript:submitForm2();" And finally two submit buttons for this form each call different js function which populate the result. So I checked out the docs and I did not find any info about selectlists.

三个动态变化的选择列表相互关联,因此我检查了文档,但没有找到有关选择列表的任何信息。

Three dynamically changing select lists interconnected to each other

<选择名称= s1 onChange = Javascript:submitForm2(); style = width:150px width = 150 >
<选择名称= s2 onChange = Javascript:submitForm2(); style = width:300px width = 300>
< select name = s3 style = width:300px width = 300>

该表单有两个提交按钮

尝试使用以下代码 this.click('select#s1 option [value = 26]'); this.debugHTML();

给我这个错误 CasperError:无法在不存在的选择器上调度click事件:select# s1 option [value = 26]

我也尝试了 document.querySelector('select [name = s1]')。setAttribute('value', 26);
给出 TypeError:'null'不是对象(正在评估'document。 querySelector('select [name = s1]')。setAttribute')

推荐答案

共享解决方案脚本。
我遍历选择列表 n * n * n 次以及日期和两个按钮。

Sharing the solution script.I Iterated upon the select lists n*n*n times along with dates and two buttons.


    require 'rubygems'
    require 'capybara-webkit'
    require 'capybara/dsl'
    require 'nokogiri'

    include Capybara::DSL
    Capybara.current_driver = :webkit

    visit("http://thewebsite.com")

    op0_xpath = "//*[@name='selectlist0']/option[1]"
    op0 = find(:xpath, op0_xpath).text
    select(op0, :from => 'selectlist0')

    page.evaluate_script("$('body').submitForm2()")
    sleep(2)

    op1_xpath = "//*[@name='selectlist1']/option[1]"
    op1 = find(:xpath, op1_xpath).text
    select(op1, :from => 'selectlist1')

    page.evaluate_script("$('body').submitForm2()")
    sleep(2)

    op2_xpath = "//*[@name='selectlist2']/option[1]"
    op2 = find(:xpath, op2_xpath).text
    select(op2, :from => 'selectlist2')

    sleep(2)

    find(:xpath, "//input[@name='curYear']").set "2012"
    find(:xpath, "//input[@name='curMonth']").set "10"
    find(:xpath, "//input[@name='curDay']").set "09"

    click_button('button1')
    page.evaluate_script("$('body').submitForm()")

这篇关于CasperJS动态选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:19