【问题标题】:Posting data on website using Mechanize Nokogiri Selenium使用 Mechanize Nokogiri Selenium 在网站上发布数据
【发布时间】:2015-08-22 04:56:39
【问题描述】:

我需要通过程序在网站上发布数据。 为了实现这一点,我正在使用 Mechanize Nokogiri 和 Selenium。 这是我的代码:

 def aeiexport
        # first Mechanize is submitting the form to identify yourself on the website
         agent = Mechanize.new
        agent.get("https://www.glou.com")
              form_login_AEI = agent.page.forms.first
              form_login_AEI.util_vlogin = "42"
              form_login_AEI.util_vpassword = "666"
              # this is suppose to submit the form I think
              page_compet_list = agent.submit(form_login_AEI, form_login_AEI.buttons.first)
              #to be able to scrap the page you end up on after submitting form
              body = page_compet_list.body
              html_body = Nokogiri::HTML(body)
             #tds give back an array of td
              tds = html_body.css('.L1').xpath("//table/tbody/tr[position()>1]/td")
            # Checking my array of td with some condition
              tds.each do |td|
                link = td.children.first             # Select the first children
                if link.html = "2015 32 92 0076 012"   
                 # Only consider the html part of the link, if matched follow the previous link
                  previous_td   = td.previous
                  previous_url = previous_td.children.first.href
                  #following the link contained in previous_url
                  page_selected_compet = agent.get(previous_url)
                  # to be able to scrap the page I end up on
                  body = page_selected_compet.body
                  html_body = Nokogiri::HTML(body)
                  joueur_access = html_body.search('#tabs0head2 a')
                  # clicking on the link
                  joueur_access.click
                  rechercher_par_numéro_de_licence = html_body.css('.L1').xpath("//table/tbody/tr/td[1]/a[1]")
                  pure_link_rechercher_par_numéro_de_licence = rechercher_par_numéro_de_licence['href']
                  #following pure_link_rechercher_par_numéro_de_licence
                  page_submit_licence = agent.get(pure_link_rechercher_par_numéro_de_licence)

                   body_submit_licence = page_submit_licence.body
                   html_body = Nokogiri::HTML(body_submit_licence)
                    #posting my data in the right field
                    form.field_with(:name => 'lic_cno[0]') == "9511681"

1) 到目前为止你对这段代码有什么看法,你认为那里有错误吗 2)这部分是我真的不确定的部分:我已经在正确的字段中发布了我的数据,但现在我需要提交它。问题是我需要点击的按钮是这样的:

<input type="button" class="button" onclick="dispatchAndSubmit(document.JoueurRechercheForm, 'rechercher');" value="Rechercher">

它触发了一个javascript函数onclick。我正在尝试 Selenium 来触发点击事件。然后我最终进入另一个页面,我需要再点击几次。我试过这个:

  driver.find_element(:value=> 'Rechercher').click
                driver.find_element(:name=> 'sel').click
                driver.find_element(:value=> 'Sélectionner').click
                driver.find_element(:value=> 'Inscrire').click

但是到目前为止我还没有成功发布数据。 你能否告诉我硒是否能让我做我需要做的事情。如果可以吗?

【问题讨论】:

    标签: ruby selenium web-scraping nokogiri mechanize


    【解决方案1】:

    一目了然,您的代码可以使用更少的缩进和更多的空格/空行来分隔AEIexport 的内部逻辑(应该更改为aei_export,因为Ruby 使用蛇形大小写方法名称。你可以找到更多关于如何设置 ruby​​ 代码样式的建议here)。

    除了您的代码样式之外,我在您的方法开头发现的一个错误是在定义form_login_AEI 时使用了未定义的变量page

    关于你的第二个问题,我对 Selenium 不熟悉;但是,由于它确实使用了真正的 Web 浏览器,因此它可以处理 JavaScript。 Watir 是另一种可能的解决方案。

    另一种方法是查看页面源代码(即在 Firebug 中)并了解页面上的 JavaScript 的作用。然后使用 Mechanize 手动点击链接。

    【讨论】:

      猜你喜欢
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多