python mechanize click on button - python

I would like to click on button "query"
my inspect element gives the following code for the button query and exit.
<TD class=font9 colSpan=4 align=center> int pageSize <INPUT class=bg0 size=3 value=10 name=pageSize> Article <!-- <input type="button" class="imagesButton" name="btnPrint" value="打 印" onclick="doPrint(gFormName);">--> <INPUT onclick=doFind(gFormName); class=imagesButton type=button value=query name=Submit> <!-- <input type="button" class="imagesButton4" name="Submit2" value="yesQuery">
--><INPUT onclick=doExit() class=imagesButton type=button value=exit name=Submit5> </TD>
I tried the below code for the click
import mechanize
br = mechanize.Browser()
submit_response = br.submit(name='Submit',label= 'query')
while executing the code I faced an error
File "C:\Python27\lib\site-packages\mechanize\_form.py", line 3185, in _find_control
raise ControlNotFoundError("no control matching "+description)
mechanize._form.ControlNotFoundError: no control matching name 'Submit', kind 'clickable', label 'query'
can any on help me how to click on button

the buttons trigger javascript functions ("doPrint(gFormName)",...)
on http://bytes.com/topic/python/answers/764326-using-mechanize-python-navigate-website is a similar problem. it was solved by triggering the javascript action of the button manually
br.form.action="button_action"
see also http://www.stackoverflow.com/questions/1806238/mechanize-python-click-a-button

Related

Can't click a button inside a <td> element

#This issue was resolved becouse it was an iframe issue, that's why the element couldn't be found, the code I used to make it work is:
button = driver.find_elements_by_css_selector(".btn.btn-primary")
I have the following html code:
<td>
<input type="hidden" value="xxxxx" id="1_nombre_77074118_1">
<input type="hidden" value="xxx#xxx.cl" id="1_email_77074118_1">
<a class="btn btn-primary" href="" onclick="javascript:submitEntrar(true,1,77074118,'1');return false;">Ingresar como usuario</a>
</td>
I need to click the class="btn btn-primary / Ingresar como usuario" button, but can't seem to find the element?
This is my code:
button = driver.find_elements_by_class_name("btn btn-primary")
print(len(button))
for b in button:
print(button)
(Both print's don't return element's becouse they where not found).
I also tried by XPATH but it didin't work, this is the XPATH:
//*[#id="container"]/section/div[1]/table/tbody/tr[1]/td[3]/a
Full XPATH:
/html/body/div[1]/div[1]/section/div[1]/table/tbody/tr[1]/td[3]/a
Thanks a lot!
First of all since there are multiple class names in the element instead of
button = driver.find_elements_by_class_name("btn btn-primary")
Try
button = driver.find_elements_by_css_selector(".btn.btn-primary")
Also you possibly have to add delay / wait before accessing these elements.
Try
button = driver.find_elements_by_css_selector(".btn.btn-primary)
can you try this as we can search and click on the element based on the text
driver.find_element_by_xpath("//a[text()='Ingresar como usuario']")
driver.find_element_by_xpath("//*[contains(text()='Ingresar como usuario')]")
driver.find_element_by_partial_link_text("Ingresar como usuario")
Also, recommend using explicitWait
button = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Ingresar como usuario']")))
button.click()
It is because of two reasons
Class call should prepend with . Eg) .btn
You are trying to call with two classes. If you notice btn and btn-primary are two different classes, so you could call using either
button = driver.find_elements_by_css_selector(".btn.btn-primary")
Or
button = driver.find_elements_by_class_name(".btn.btn-primary")

Python & Selenium - Find element by label text

Im trying to locate and click an element (checkbox) from a big selection of checkboxes on a html site using python and selenium webdriver. HTML code looks like this:
HTML Code
<div class="checkbox-inline col-md-5 col-lg-3 col-sm-6 m-l-sm rightCheckBox">
<input type="checkbox" checked="checked" class="i-checks" name="PanelsContainer:tabsContentView:5:listTabs:rights-group-container:right-type-view:2:right-view:2:affected-right" disabled="disabled" id="id199"> <label>Delete group</label>
</div>
My problem is that the only unique identifier is:
<label>Delete group</label>
All other elements/id's/names are used by other checkboxes or changes from page to page.
I have tried the following code:
driver.find_element_by_xpath("//label[contains(text(), 'Delete group')]").click()
But I only get error when using this.
Error: element not interactable
Anyone able to help with this?
Try the below xpath
//label[contains(text(), 'Delete group')]//ancestor::div//input
Try with Javascript.
checkBox = driver.find_element_by_xpath("//label[text()='Delete group']//ancestor::div//input")
# Scroll to checkbox if its not in screen
driver.execute_script("arguments[0].scrollIntoView();", checkBox)
driver.execute_script("arguments[0].click();", checkBox)
Note : As per HTML shared by you, checkbox is in Disabled state, so i am not sure click will trigger any action. However above code will click your checkbox.

How to fix Selenium not clearing default text on an input

I am trying to input an amount in a text field which contains a default string.
HTML of that field:
<div class="InputGroup">
<span class="InputGroup-context">$</span>
<input autocomplete="off" class="Input InputGroup-input" id="amount" name="amount" type="text" maxlength="12" value="0.00">
</div>
When trying to input text to the field, instead of replacing the default text, it appends it to it.
I have tried to use amount.clear() (amount is what I am calling the element) but after running that and sending the keys it throws the below exception:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
As you will see from my current code, I am also trying to double click the default text but that doesn't help.
This is my code at the moment:
ActionChains(driver).move_to_element(amount).click(amount).click(amount).perform()
amount.send_keys(Keys.BACKSPACE)
amount.send_keys('100')
Which results in an input field of 0.00100 when I'm expecting 100.
Some JS based webs apps may not clear the input fields properly.
You can try using Actions class to click and clear a field fully. For example:
elem = driver.findElement(By.id("amount"))
actions.move_to_element(elem).click().build().perform()
actions.send_keys(Keys.BACKSPACE)
.send_keys(Keys.BACKSPACE)
.send_keys(Keys.BACKSPACE)
.send_keys(Keys.BACKSPACE).build().perform()

selenium : Click on dynamic radio button using python

I'm trying to click on a radio button on a web page which is dynamic. I tried classname, xpath and nothing works.
The code that contains the radio button is :
<input id="radiofield-1247-inputEl" class="x-form-field x-form-radio x-form-cb" type="button" hidefocus="true" autocomplete="off" role="radio">
There is a label which is adjacent to the button.
<label id="radiofield-1247-boxLabelEl" class="x-form-cb-label x-form-cb-label-after" for="radiofield-1247-inputEl">Yes</label>
I tried :
driver.find_element_by_xpath("//*[#type='radio'][#class='#class here'") #.click() and this doesn't work.
I tried passing the absolute xpath, which also fails. There is no name or id to do a search for.
You're trying type="radio" while on provided HTML it's type="button"...
Try following XPath:
driver.find_element_by_xpath('//input[contains(#id, "radiofield-")][#role="radio"]')

Python-Selenium won't click button

The webpage i'm starting from is https://classschedule.tulane.edu/Search.aspx . The page source information for the button I need clicked is:
<input type="submit" name="ctl00$MainContent$btnSearchAll" value="All Courses" id="btnSearchAll" class="JQButton ui-button ui-widget ui-state-default ui-corner-all" role="button" aria-disabled="false" autocomplete="off" style="height: 22px;">
I have tried different methods to find this button and click on it such as;
element = browser.find_element_by_id("btnSearchAll")
element = browser.find_element_by_xpath("//input[#id ='btnSearchAll']")
element = browser.find_element_by_name("ctl00$MainContent$btnSearchAll")
I think it is finding the button because when I do...
print element
...this is returned:
<selenium.webdriver.remote.webelement.WebElement object at 0x2b49690>
I have no other ideas on how to make my program click the button.
I think you didn't add the code for clicking on the element. Please do that and check:
element = browser.find_element_by_id("btnSearchAll")
element.click()
I did try the same code as above with JAVA, and it is working fine.
This works well for me
for i in range(1, 10):
try:
driver.find_element_by_xpath(
f'/html/body/div[{i}]/div/div/div/div/div[2]/span[1]/span/span/input').click()
except:
pass

Categories