最简单的方式来“浏览”到一个页面并用Java提交表单 [英] Easiest way to "browse" to a page and submit form in Java

查看:148
本文介绍了最简单的方式来“浏览”到一个页面并用Java提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做的是浏览到网页,登录,然后浏览到该网站上的另一个网页,要求您登录,因此需要保存cookie。之后,我需要点击该页面上的一个元素,在那里填写表单并获取网页返回给我的消息。我需要真正进入页面并单击该按钮的原因是因为假设您只是直接导航到链接,这是因为每次登录时都会为您分配一个会话ID,并单击该链接,并且该链接始终不同。按钮看起来像这样,它不是一个正常的href链接:

 < span id =:tvidlink = class =sAtabindex =0role =link>下一步< / span> 

无论如何,最简单的方法是做什么?感谢。

更新:
尝试使用HTMLunit和其他无头浏览器库后,似乎并没有发生任何无头的事情。我最近发现关于这个页面的另一件事是,所有的HTML格式都是奇怪的格式......它全部放在脚本标签中。这里是一个示例。

 ?ui\x3d2\x26view\x3dss\x26mset\x3dmain\x26ver \x3d-68igm85d1771\x26am\x3d!ZSL-0RZ-XLv0BO3aNKsL0sgMg3nH10t5WrPgJSU8CYS-KNWlyrLmiW3HvC5ykER_n_5dDw\x26fri ], http://example.com/?ctx\x3d%67mail\x26hl\x3den ,, 0,Gmail,Gmail,[[us,c130f0854ca2c2bb,[[n],[m,新功能!],[u],[k 0],[ p, 1000:500000,10,200000,5,100000,3,75000,2,0,1],[ H,https://survey.googleratings.com /wix/p1679258.aspx?l\x3d1033\"],[\"at\",\"query,5,contacts,5,adv,5,cf,5,default,20\"],[\"v\",\"https: //www.youtube.com/embed/Ra8HG6MkOXY?showinfo\x3d0],

当我检查按钮上的元素,我在上面为按钮发布的HTML代码出现,但不是在查看源代码时。基本上,我需要做的是使用某种图形用户界面,并让用户导航到链接,然后让程序填写信息。有谁知道我该怎么做?感谢。

解决方案

这应该可以在Selenium中使用,正如其他人注意到的那样。



我使用Selenium登录,然后抓取网站,并发现网站上每种表单的值(30+表单)。这些值稍后用于填写和提交具有特定值的值的表单。这个网站是非常JS / jQuery沉重,我用Selenium的内置支持javascript执行程序,css选择器和XPath来完成此操作。



我将HtmlUnit和HttpUnit实现为更快的选择,但发现它们并不像Selenium那样可靠,因为我正在抓取的网站的JS语义。



很难告诉你如何完成它的代码,因为你的Selenium实现将是相当页面特定的,我不能看你正在编码的页面,以找出那个按钮脚本垃圾发生了什么。但是,我已经包含了一些可能相关的selenium代码(Java)片段:

  Element element = driver.findElements(By.id值)); //在页面
上查找元素List< Element> buttons = parent.findElements(By.xpath(./ tr / td / button)); //找到子元素
button.click();
element.submit()//提交封闭表单
element.sendKeys(text); //输入文本中的
字符串elementText =(String)((JavascriptExecutor)驱动程序).executeScript(return arguments [0] .innerText || arguments [0] .textContent,element); //通过JS
与$ selenium元素交互

如果您在不同页面上编写类似的函数,一个href =http://code.google.com/p/selenium/wiki/PageObjects =nofollow> PageObjects 可以提供帮助。



Anew发布的链接是一个很好的起点,而好的StackOverflow可以解决任何Selenium问题。


What I need to do is browse to a webpage, login, then browse to another webpage on that site that requires you to be logged in, so it needs to save cookies. After that, I need to click an element on that page, in which I would fill out the form and get the message that the webpage returns to me. The reason I need to actually go to the page and click the button as suppose to just navigating directly to the link is because the you are assigned a session ID every time you log in and click the link, and its always different. The button looks like this, its not a normal href link:

<span id=":tv" idlink="" class="sA" tabindex="0" role="link">Next</span>

Anyway, what would be the easiest way to do this? Thanks.

Update: After trying HTMLunit, and other headless browser libraries, it doesnt seem that its happening using anything "headless." Another thing that I recently found out about this page is that that all the HTML is in some weird format... Its all inside a script tag. Here is a sample.

"?ui\x3d2\x26view\x3dss\x26mset\x3dmain\x26ver\x3d-68igm85d1771\x26am\x3d!Zsl-0RZ-XLv0BO3aNKsL0sgMg3nH10t5WrPgJSU8CYS-KNWlyrLmiW3HvC5ykER_n_5dDw\x26fri"],"http://example.com/?ctx\x3d%67mail\x26hl\x3den",,0,"Gmail","Gmail",[["us","c130f0854ca2c2bb",[["n"],["m","New features!"],["u"],["k","0"],["p","1000:500000,10,200000,5,100000,3,75000,2,0,1"],["h","https://survey.googleratings.com/wix/p1679258.aspx?l\x3d1033"],["at","query,5,contacts,5,adv,5,cf,5,default,20"],["v","https://www.youtube.com/embed/Ra8HG6MkOXY?showinfo\x3d0"],

When I do inspect element on the button, the HTML code that I posted above for the button comes up, but not when doing view source. Basically, what I am going to need to do is use some sort of GUI and have the user navigate to the link and then have the program fill out the info. Does anyone know how I can do this? Thanks.

解决方案

This should be possible in Selenium as others have noted.

I have used Selenium to login then crawl a site and discover every permuation of values for every form on the site (30+ forms). These values are later used to fill and submit the form with a specific perumation of values. This site was very JS/jQuery heavy and I used Selenium's built-in support of javascript executor, css selectors, and XPath to accomplish this.

I implemented HtmlUnit and HttpUnit as faster alternatives, but found they were not as reliable as Selenium given the JS semantics of the site I was crawling.

It's hard to give you code on how to accomplish it because your Selenium implementation will be quite page-specific and I can't look at the page you're coding against to figure out what's going on with that button script junk. However, I have include some possibly relevant selenium code (Java) snippets:

Element element = driver.findElements(By.id(value)); //find element on page
List<Element> buttons = parent.findElements(By.xpath("./tr/td/button")); //find child element
button.click();
element.submit() //submit enclosing form
element.sendKeys(text); //enter text in an input
String elementText = (String) ((JavascriptExecutor) driver).executeScript("return arguments[0].innerText || arguments[0].textContent", element); //interact with a selenium element via JS

If you are coding similar functions on different pages, then PageObjects behind interfaces can help.

The link Anew posted is a good starting point and good ol' StackOverflow has answers to just about any Selenium problem ever.

这篇关于最简单的方式来“浏览”到一个页面并用Java提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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