How to select the correct submit button with mechanize for Python? - mechanize-python

I'm trying to submit a form (using mechanize in Python) that has two submit buttons as shown below.
<input type="submit" value="Save Changes " name="SaveChanges">
<input type="submit" value="Reboot" name="SaveChanges">
Mechanize "print control" shows this...
<SubmitControl(SaveChanges=Save Changes )>
<SubmitControl(SaveChanges=Reboot)>
How do I select the "Reboot" submit button with mechanize? I've tried:
br.submit()
br.submit("Reboot")
br.submit("SaveChanges=Reboot")
The correct form is selected, but none of these submit options are working. I'm new to Python and would appreciate any help.

I just figured it out.
br.submit(nr=1)
will select the second submit button (nr count starts with zero)

Related

Xpath Changes regularly

I have an issue. When I select an xpath and run the test sometimes the first few times it works. After some time it fails, and when i go check the xpath again i discover that somethings in it has changed. This isn't a web application that is being updated constantly . What do you think the problem could be?
for example the name in the code changes regularly . Here it is turnOverAvailableInd1 later it may become turnOverAvailableInd2.
<td>Is turnover figure available?</td>
<td valign="baseline">
<input type="radio" name="turnOverAvailableInd1" value="Y" onclick="javascript:turnOverAvailableToggle(this);" id="turnOverAvailableInd1Yes">YES
<input type="radio" name="turnOverAvailableInd1" value="N" onclick="javascript:turnOverAvailableToggle(this);" id="turnOverAvailableInd1No">NO
</td>
This is how i select the radio button
Select Radio Button turnOverAvailableInd1 N
You can use the other locator like, ID, CSS Selector or different XPATH like below
//input[#id='turnOverAvailableInd1Yes'] // For YES radio button
//input[#id ='turnOverAvailableInd1No'] //For NO radio button
OR
You can use the Value like below
//input[#value='Y'] //For YES radio button
//input [#value= 'N'] //For NO radio button
OR
//*[contains(text()='YES')] //For YES radio button
//*[contains(text()='NO')] //For NO radio button
You can find more here
If I understand your question correctly, you want an xpath that can work whether then name is "turnOverAvailableInd" appended by some number. If so, the following xpaths could work:
//input[#value='Y' and contains(#name,'turnOverAvailableInd')]
//input[#value='N' and contains(#name,'turnOverAvailableInd')]

Selecting from 2 Radio buttons when 1 button has no id - Webdriver

I am trying to write a script in web driver and part of the page that I am testing has 2 radio buttons where the person has to agree that something is correct.
By default the no button is selected and the user has to select the yes radio button to continue.
I have read a number of solutions for things similar and I've tried numerous different things but still can't get Selenium to select the yes radio button. It is a Yes/No question. The No field is already pre-populated but I need it to select Yes.
As you can see below there is a small amount of code for this part of the form.
<pre>
<form action="">
<input id="selfCert" name="selfCert" value="no" checked="checked" type="radio">No
<input name="selfCert" value="yes" type="radio">Yes
</form>
</pre>
I have tried to use the xpath and the css but the test just terminates.
Any advice would be greatly appreciated.
Thanks
Gareth
Can u use xpath to select the Yes radio button?
As both input fields share the same name="selfCert" I think u need to use xpath then.
I have an example in C# for Selenium, if you're using a different language you will need to modify it.
YesRadioButton = webDriver.FindElement(By.XPath("//form/input[2]"));
YesRadioButton.Click();
I have managed to resolve the issue now using a wait command until webdriver can see the button
WebElement element = driver1.findElement(By.xpath("/html/body/div[1]/div[2]/div[2]/div[5]/div[1]/div/form/input[2]"));
WebDriverWait wait = new WebDriverWait(driver1, 20);
wait.until(ExpectedConditions.visibilityOf(element));
element.click();

className, tagName etc in selenium java

I tried to click the button using selenium.
The code of the button is as follows:
<button class="btn btnSearch" type="button" data-gtm="wyszukiwarka" szukaj">Szukaj</button>
I do not see any id?
It's possible to click the button like this?
While there are many ways to click a button without name and ID, and there are many duplicates of this question, my advise is always to use a relative XPath expression when there is no name or ID.
You can click the button with:
findElement(by.xpath("//button[#class='btn btnSearch']"))
As per the HTML provided to click on the button with text as Szukaj you can use the following line of code :
Java :
driver.findElement(By.xpath("//button[#class='btn btnSearch' and contains(.,'Szukaj')]")).click();
Python :
driver.find_element_by_xpath("//button[#class='btn btnSearch' and contains(.,'Szukaj')]").click()
Dot Net :
driver.FindElement(By.XPath("//button[#class='btn btnSearch' and contains(.,'Szukaj')]")).Click();

scraping authentication page with multi form

On the site I want scraping there an authentication page.
there is no username and password to enter but among twenty click button to select a location.
example the form of a button:
<form id="liliForm-2519" name="liliForm-2519" method="post" action="http://exemple.com/rat/body.ayers.verlayer/57">
<input type="hidden" name="t:formdata" value="ERERAAADFDFvzloEVAN3OqfcAA">
<input class="submit" type="submit" value="Acces">
</form>
is that it is possible to simulate the clik for the submit? And if so could someone help me? Thank you in advance STEF
You need to specify the clickdata parameter in the FormRequest class:
http://doc.scrapy.org/en/latest/topics/request-response.html#formrequest-objects
def parse_page(self,response):
return FormRequest.from_response(response,clickdata={'value':'Acces'})
or you can use the formxpath parameter to match a certain xpath.
If you want me to further look into it, feel free to post a link.
with python scrapy you can do something like
def parse_page(self, response):
FormRequest(url=http://exemple.com/rat/body.ayers.verlayer/57",
method="POST",
response=reponse)
it will retrieve actual values from your html reponse to fill in missing input fields of the form.

Submitting Html Form with Python's Mechanize - two submit buttons no IDs

I am trying to submit a HTML button on a PHP/HTML page that contains 1 form and two different buttons that do different tasks. I need to submit the second submit button. The problem is that neither button has an id attribute and are distinguished only by their title tag.
I need to select Orders and Orderlines via a select box (which I think is correct) and press the second 'Go'/Submit button on the form.
e.g.
<form>
....
<input class="button" type="submit" value="Go">
....
then further down we have...
<select class="sfield" name="last">
<option value="0" selected="">From: First order</option>
<option value="1">From: 19/10/2012 16:22</option>
</select>
<select class="sfield" name="items">
<option value="1" selected="">Include: Orders and items</option>
<option value="0">Include: Order lines only</option>
</select>
<input class="button" type="submit" onclick="this.form.action.value='export'" title="Generate CSV file" value="Go">
My Python code snippet is as follows...
br.form['last'] = ['0'] # Select the 'From First Order'
br.form['items'] = ['1'] # Select both Orders and Order Lines
br.submit()
# When I uncomment the line below I can process a CSV file but it just contains Orders only!
# br.open("http://www.mysite.com/admin/ordermanager.php?action=export")
# Read and process CSV file from csv link displayed on repost of submitted form....
# this section below works fine.
for link in br.links(url_regex=r"export",nr=0):
resp = br.follow_link(link)
content = resp.read()
Last_Run_Date = strftime("%Y%m%d%H%M%S", localtime())
with open('c:\python27\%s.csv' % Last_Run_Date, 'w') as fo:
fo.write(content)
There does not seem to be a way of specifying the title attribute in the br.submit such as
br.submit(title='Generate CSV file') in the Mechanize documentation.
I am assuming that br.submit() is selecting the first submit control when I want the second?
Does anyone know of a clever way around this?
you could use the number of the submit button:
br.submit(nr=2)
nr starts from 0 for the first submit button