selenium not setting input field value - python

Let's say we have this website https://www.coinichiwa.com/ which has a BET AMOUNT input box. It's html is:
<input autocomplete="off" id="betFa" name="a" maxlength="20" value="0.00000000" class="betinput" style="">
I need to add some value into it. Here is my code:
browser = webdriver.Firefox()
browser.get('https://www.coinichiwa.com')
browser.find_element_by_id("betFa").send_keys("0.00000005")
print browser.find_element_by_xpath("//input[contains(#id,'betFa')]").text
But it's neither setting it's value to "0.00000005" nor it printing the value of input.
I'm not sure what's going wrong. Can you suggest?
Why it's not working?

You need to clear() the text input first:
bet_fa = browser.find_element_by_id("betFa")
bet_fa.clear()
bet_fa.send_keys("0.00000005")
As for the your second problem - this is an input and the value you enter into it is kept inside the value attribute, not the text. Use get_attribute() method:
browser.find_element_by_xpath("//input[contains(#id,'betFa')]").get_attribute('value')

Related

Python selenium - set value using data-qa attribute

I have the following object,
<div class="form-group">
<label data-qa="username-label"><span id="i18n-2">Username</span></label>
<input id="ember4124" class="ember-view ember-text-field form-control" data-qa="username-input" type="text" value="">
</div>
Here the input id "ember4124" changes every time. My requirement is find by data-qa "username-input" and pass the value, where value is the username.
Using below I can find the element,
driver.find_elements_by_css_selector("input[data-qa='username-input']")
Output: [<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="90144b65-8a86-4d54-90ae-4ccccab0cc9e", element="18e84265-46a7-41d8-a487-299e95ef7ae8")>]
But not sure how to send the value. Can anyone assist me?
Probably there is only one element so use element (without s at the end) instead of elements to get only first element and you will have
x = driver.find_element_by_css_selector("input[data-qa='username-input']")
x.send_keys("James Bond")
If you will use elements (with s at the end) then you get list and you will have to use [0] to get first element on list
x = driver.find_elements_by_css_selector("input[data-qa='username-input']")
x[0].send_keys("James Bond")

How to get value from date filter input field with Selenium

There is a date filter with input fields and drop-down datepicker like on below image
I want to get current value of, for example, "From" input field (expected output = "03/13/2013"). So for following element's code
<div class="input-group date" id="inputValidFrom">
<input name="validFrom" class="form-control" id="inputValidFromValue" required="" type="text"> </input>
I use Python's lines:
>>>from selenium import webdriver
>>>driver = webdriver.Ie()
>>>input = driver.get_element_by_xpath('//input[#name="validFrom"]')
>>>input.text # returns empty string
''
>>>input.get_attribute('value') # also returns ''
''
>>>input.value_of_css_property('text') # returns again just empty string
''
Who knows the way how to get this input field box property?
You are using the IE-Driver with XPath.
Try writing the attribute name in lower case.
http://www.seleniumhq.org/docs/03_webdriver.jsp#by-xpath

Verify input box text using Python + Selenium

I have gone through other similar questions and the general advice was to use the get_attribute() method with input 'value'.
However, this doesn't seem to work in my case.
The tag I am trying to test is -:
<input class="form-control required" id="email" name="email" placeholder="Enter your E-mail / Username" required="" type="text" value="">
and the code I am using is as follows -:
def enter_keys_by_id(self, id, text):
"""
Finds a form element by ID and then proceeds
to enter text into the form.
"""
find_element = self.driver.find_element_by_id(id)
assert find_element.get_attribute('value') == ""
find_element.send_keys(text)
assert find_element.get_attribute('value') == text
return find_element
with the function call -:
enter_keys_by_id("email", "someemail#email.com")
The 'value' attribute is set to blank in the tag. How would I go about it in this particular case?
So, you type email to the "empty" input, but I think that you need to submit the form, ie save the value to the input before retrieveing it. I guess you are trying to verify that the email was saved correctly, so you can:
Type email
Submit the form (you didn't provide html, but I guess there is some submit button near to the email field)
And after that try to assert find_element.get_attribute('value') == text (or to wait for the input with value='your text')

Selecting an unnamed text field in a mechanize form (python)

So i'm making a program to batch convert street addresses to gps co-ordinates using mechanize and python. this is my first time using mechanize. I can select the form ("form2') on the page. however the text box in the form has no name. how do i select the textbox so that mechanize can enter my text? I've tried selecting it by its id. but that does not work.
br.select_form("Form2") #works as far as i know
br.form["search"] = ["1 lakewood drive, christchurch"] #this is the field that i cannot select
and here is the source code from the website.
<form name="Form2" >
or Type an <b>Address</b>
<input id="search" size="40" type="text" value="" >
<input type="button" onClick="EnteredAddress();" value="Enter" />
</form>
any help would be much appreciated.
form.find_control(id="search") ?
FWIW I solved this by using the above answer by lazy1 except that I was trying to assign a value after using the find_control method. That didn't work of course because of assignment, I looked deeper into the method and found setattr() and that worked great for assigning a value to to the field.
will not work
br.form.find_control(id="field id here") = "new value here"
will work
br.form.find_control(id="field id here").__setattr__("value", "new value here")

Use getControl to control objects other than the name variable

I am using the Zope testbrowser which has been recommended in my last question. The problem that I am facing is that I can use the getControl function to control different objects like: password, username etc.
I am trying to submit the page to get to the next page but the submit button has no 'name' variable, just an 'id' variable. 'Submit' is written as follows:
<input type="submit" id="lgn_button" class="button" tabindex="3" accesskey="s" />
and the other objects are written as:
<input type="password" class="button" name="password" id="password" size="24" maxlength="20" accesskey="p" tabindex="2" value=""/></td>
I have no access to change this. The python zope code I am using to gain control of the 'password' object is:
browser.getControl(name='password')
The submit button doesn't have 'name' so I have written:
browser.getControl(id='lgn_button')
This prints out the error that 'id' is invalid:
TypeError: getControl() got an unexpected keyword argument 'id'
Is there any way to gain control of one of the other values in 'submit'.
Thanks for any help.
I assume that, for one reason or another, you can't add a 'name' attribute to your tag, but if it's only a 'name' that you can't add, you can explicitly set a 'value=Submit' (instead of relying on the default one, which is Submit) and then use browser.getControl('Submit')
Failing that, you can do something along the lines of
for form in browser.mech_browser.forms():
for control in form.controls:
if control.id == 'lgn_button':
return control
You might even want to extend Browser.getControl() with that and contribute it back to zope.testbrowser. ;)

Categories

Resources