简体   繁体   中英

Why does my XPath not return anything?

I am trying to get an XPath of a number input text box as well as a submit button from the page http://vacancycollection.nic.in/cglmarks/ .

I created the XPath using Firebug's Copy XPath option .

Here is my XPath for input text field:

HtmlTextInput rollNum = (HtmlTextInput)index.getFirstByXPath("/html/body/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr[2]/td/input");

The XPath for the button is:

HtmlButton button = index.getFirstByXPath("/html/body/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr[5]/td/button");

But when I try to access the text as well as the button field, it throws a java.lang.NullPointerException exception when I try to look at the code with System.out.println(rollNum.toString()); .

I am using HTMLUnit with Java to insert data into the input fields and click the submit button. It feels like there is some error in the XPaths themselves. What should be the correct XPaths for the elements and how to find them?

I figured it out. Actually the xpath was not working because i was trying to access elements which was getting generated by my javascript code but the webclient was not waiting for the elements to load hence java.lang.NullPointerException exception was thrown. I added webClient.waitForBackgroundJavaScript(30 * 1000); to wait the webClient for 30 sec and now i can access the element using same xpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM