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')
Related
I want to write a test that checks if the correct text is shown on a website. It mostly works, but I am stuck with checking the label of a checkbox.
HTML:
<div class="checkbox">
<label>
<input id="remember_me" name="remember_me" tabindex="130" type="checkbox" value="y">
" Remain logged in
"
</label>
</div>
Behave:
Scenario: See the formular for log in
Given I am on the "/login" page
Then I should see header "Log In"
...
And I should see the checkbox for "remember_me"
And I should see label "Remain logged in"
The header is found without issue:
#then(u'I should see header "{header}"')
def step_impl(context, header):
assert context.browser.find_element_by_xpath("//h1[contains(text(), '{}')]".format(header))
as well as the checkbox itself:
#then(u'I should see the checkbox for "{name}"')
def step_impl(context, name):
assert len(context.browser.find_elements_by_xpath("//input[#type='checkbox'][#name='{}']".format(name))) > 0
I don't know how to specifically check for the label "Remain logged in" however. Anything I tried, including checking for the text itself with find_text() or in such a way:
def step_impl(context, label):
assert context.browser.find_element_by_xpath("//input[#type='checkbox' and contains(text(), '{}')]".format(name))) > 0
has not work.
Does Selenium allow to check the text of the label and if yes, how would I go about it?
Are you locating label element correctly? the following code should return the text from the label webelement.
label_element = browser.find_element_by_xpath("//div[#class='checkbox']/label")
TextValue = label_element.text
or
TextValue = label_element.get_attribute('textContent')
I have this form
<form class="form-inline" type="get" action=".">
<input id="excel_input" class="form-control" type="text" name="excel_input" value="0">
<input class="btn" type="submit" name="excelbutton" value="excelbuttonclicked"
onclick='document.getElementById("excel_input").value = "1";'><i class="ion ion-search"></i></input>
</form>
I want to get the value "1" when the excelbutton clicked in the Get request in order to export an excel file. I want to use the Get request because in this point I have a ready query set , the query set user sees on the screen.
I use the commands
request.GET.get('excel_input', None)
if excel_string not in ['0', None]:
testxlsxwriter(d_list)
to get the value of the input field and export the excel file. The problem is that I get the value "1" from the input field on button click , but then I always get the value "1" on get requests( refresh , next page etc.
I see the value "0" on screen on refresh but I get the value "1" on my view
I also tried javascript with no luck
window.onload = function(){
document.getElementById("excel_input").value = "0";
}
Can someone help me please to understand how get request works with this input element?
Thanks a lot
Kostas
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')
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")
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. ;)