Hide the text input field portion in a form's GET query url when the value is an empty string - html

e.g.: http://127.0.0.1:8000/database/?reference_doi=&submit=Submit
I know It appears to be an html standard, but is there a tag to switch it, so that the empty text input string does not appear in the query url?
Or alternatively, since I'm using Django, I tried doing the following in my view.
request_get_copy = request.GET.copy()
for key, value in request_get_copy.items():
if not value or key == 'submit':
request_get_copy.pop(key)
request.GET = request_get_copy
request.META['QUERY_STRING'] = request_get_copy.urlencode()
I displayed request.GET and request.META['QUERY_STRING'] in the actual page through my template, and several methods that request object has, and they all gave successfully "corrected" values, like http://127.0.0.1:8000/database/ But since the GET request first goes through the browser, the displayed url still contains empty string value portions. Is there anything I can do?

The easiest thing you could do is to issue a redirect to your fixed URL:
fixed_url = request_get_copy.urlencode()
return redirect(fixed_url)
Even better if you do that only if it actually changed, and before any DB access or heavy work.
This means an additional GET, but gives you the result you want, and I guess that's more valuable to you :)
If Javascript is an option, you could also do this changes before the submit actually happens, it's a tad more convoluted but will avoid the extra request.
Edit: Just to be clear, there's no way to "turn it off", you could say this is how HTTP and browsers work :)

Related

How can I store an XPath as a variable in Python Selenium?

I want to be able to select text on a website, store it, and then turn that stored information into a variable. More specifically as seen in the picutre below, my goal is for the selected name, in this case, John J. John, somehow be copied, turned into a variable, and then printed, all without emulating any key binds.
HTML Inspect element photo
The code I have tried to use to get the information is this:
selectedName = browser.find_element_by_xpath('//*[#id="sign_in_box"]/div/div[2]/div/div[1]/div').get_attribute("div")
print (selectedName)
The return I am getting is this:
None
I know that the problem almost definetly lies somewhere in the path, but I can't figure it out.
Assuming your xpath is correct, you should put .text instead of .get_attribute("div") since it will not return the text you want, but the div itself.
How to get text with Selenium WebDriver in Python
Try
selectedName = browser.find_element_by_xpath('//*[#id="sign_in_box"]/div/div[2]/div/div[1]/div').text

How do I auto-fill in this textbox at the US govt website via the URL?

I'm trying to embed a value into the textbox at the USCIS government website to check my application status number. Suppose it's LIN1234. After inspecting the element of the webpage I see that the HTML wrapper for the textbox is:
<input id="receipt_number" name="appReceiptNum" class="form-control textbox initial-focus" maxlength="13" type="text">
I tried opening up this URL with a suffix added on, but to no avail:
https://egov.uscis.gov/casestatus/landing.do?receipt_num=LIN1234
Is there a way to to this?
Before that, you must understand what means adding ?receipt_num=LIN1234 to the url.
When sending a request (By default and in this context) from your browser, it'll be a GET request (see here) where you send as a get argument your receipt number, setting its key to receipt_num.
What is done to this data on the server side, however, is up to itself.
Just understand that unless the server is made to auto-fill the field with that value in case it receives it, it won't do anything except sending some more data.
I think you want to load this page in your browser with auto-filled field.
In that case you should look into extensions for your browser that would do that automatically.
You probably won't be able to embed a value into the textbox... Just because you are sending values by GET (which is what the landing.do?receipt_num=LIN1234 syntax is doing) doesn't mean that they have something set up to process it, so the GET variable will probably not do anything.
You might be able to see how their URLs work ordinarily, what the page URL that you are aiming to land on looks like, and either decode something from that or set a bookmark there. That said, if they are submitting that data via POST (which they probably are, for security reasons), that probably won't work.
I would suggest looking at reputable form-filling plugins for your web browser, if that's an option. That might allow you to work around that.

HTMLForm's default action

While doing Code Review on Wikimedia Gerrit, I stumbled across comments saying:
$htmlForm->setAction( wfScript() );
Reviewer: not needed, wfScript() is the default for the action.
So I consulted the documentation about HTMLForm::setAction (huge page).
Set the value for the action attribute of the form.
When set to false (which is the default state), the set title is used.
However, what I do not understand is how wfScript (Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScriptPath etc. except for 'index' and 'load' which use $wgScript/$wgLoadScript) could be extracted from the title (instance of Title?).
This doesn't make any sense to me as wfScript() returns an entry point and all Titles usually share the same entry point.
Looking up HTMLForm::getAction, I see the code really uses Title. Only conditionally, though. Simply said, if Title::getLocalURL would return a URL containing a query string, e.g. /mw/index.php?title=Special:Contributions, wfScript() is returned, and the title isn't used at all, as opposed to what is documented in HTMLForm::setAction(). The rationale is clear: This is because browsers may strip or amend the query string, which is unwanted here.
Why isn't the hidden form field approach always used and why does the Title have to know about its entry point?
How is $this->getConfig()->get( 'ArticlePath' ) related to $this->getTitle()->getLocalURL() [The former is used as a condition in and the latter is possibly returned from HTMLForm::getAction.]
I'm not totally sure I understand your question, so if this answer doesn't really answer your questions, feel free to comment on it and I'll try to fix my answer :)
Why isn't the hidden form field approach always used and why does the Title have to know about its entry point?
Why should it? It would be possible, yes, but the only reason to use it is, that browsers strip out parameters passed to the action parameter of the form. Other values (such as short urls) works fine. The other aspect is, that, if you configure short url's (e.g. yourdomain.com/wiki/Special:UserLogin instead of yourdomain.com/w/index.php?title=Special:UserLogin), why should HTMLForm use
yourdomain.com/w/index.php?title=Special:UserLogin&wpusername=test&wppassword=123 (bad example, because UserLogin doesn't use HTMLForm and wouldn't use GET, but think about any other example :P) instead of the (for the user) nicer one yourdomain.com/wiki/Special:UserLogin?wpusername=test&wppassword=123? So it doesn't have a real technical background to not use always the hidden title field, iirc.
How is $this->getConfig()->get( 'ArticlePath' ) related to $this->getTitle()->getLocalURL()
The wgArticlePath configuration variable specifies the base URL for article links, which means, if you call getLocalURL on a Title object, the config var is used to build the URL/Link if no query is specified (see the code of getLocalURL to know how it works). That means, that the config variable specifies, how links are returned from this function (e.g. /w/index.php?title=$1 or /wiki/$1). So it's a very important part for this function and (to close the circle to HTMLForm) the important condition to decide, if wfScript() is used or the local url (from the Title object), as it is the condition for Title::getLocalURL() to decide if a question mark is used or not.
I hope that helps a bit to understand what HTMLForm does, if not, feel free to comment :)

What is the difference between null and ""

I am currently trying to understand the jsp codes of another guy but I am having trouble with the following codes:
if( request.getParameter("username")!=null &&
!request.getParameter("username").equals(""))
The username is the field a user fills on an HTML form. The codes after these codes saves the data the user fills in into strings which will be later used for other purposes.
My question is what is the purpose of the !request.getParameter("username").equals("") code part in the above?
Has request.getParameter("username")!=null part of the code not already tested whether the user enters information into the input field of the HTML form or not?
Regards
If you reveive a request like
http://.../yourservlet
then the parameter value will be null, since no parameter named username is passed at all.
If you reveive a request like
http://.../yourservlet?username=
then the parameter value will be the empty string, since the parameter is passed, but its value is the empty string.
What you'll receive depends on the HTML, and on what the user actually does (he could type the URL manually in the address bar, for example). The code makes sure both cases are handled in the same way. Let's imagine some JavaScript in the page disabled the input field or removes it from the DOM when some checkbox is checked. In that case, the parameter won't be sent at all.
Has request.getParameter("username")!=null part of the code not already tested whether the user enters information into the input field of the HTML form or not?
No. A text input in a HTML form will always send a value. If nothing has been typed into it, then that value will be "".

explicitly setting "pretty print" formatting for Graph API request

When querying the Graph API with a 'known' browser the JSON output is formatted in a way to make it easy for humans to read. If the query is done with any other browser then it outputs everything on one line.
How do you explicitly make the request so that the formatting is not done? I'm sure I've seen it somewhere, but I can't seem to find it now.
The pretty parameter controls it. So https://graph.facebook.com/foo?pretty=0 always prints without extra whitespace, while https://graph.facebook.com/foo?pretty=1 always prints with extra whitespace, and omitting it causes the default behavior of switching based on user-agent.