HtmlUnit accessing an element without id or Name

谁都会走 提交于 2019-12-03 17:21:34

There are several options for an XPATH to select that input element.

Below is one option, which looks throughout the document for an input element that has an attribute named type with the value "submit" and an attribute named value with the value "Save as XML".

//input[@type='submit' and @value='Save as XML']

If you could provide a little bit more structure, a more specific (and efficient) XPATH could be created. For instance, something like this might work:

/html/body//form//input[@type='submit' and @value='Save as XML']

You should be able to use the XPATH with code like this:

client = new WebClient(BrowserVersion.FIREFOX_3)
client.javaScriptEnabled = false

page = client.getPage(url)

submitButton = page.getByXPath("/html/body//form//input[@type='submit' and @value='Save as XML']")

Although I would, in most cases, recommend using XPath, if you don't know anything about it you can try the getInputByValue(String value) method. This is an example based on your question:

// Fetch the form somehow
HtmlForm form = this.page.getForms().get(0);
// Get the input by its value
System.out.println(form.getInputByValue("Save as XML").asXml());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!