Radio button does not get clicked in Selenium / Python - python

Folks, this is driving me crazy. I have snippets like the following
<label class="" for="M37_Q_POSWECHSEL_BETT_B1">
<input id="M37_Q_POSWECHSEL_BETT_B1" name="M37_Q_POSWECHSEL_BETT" value="B1" aria-describedby="M37_Q_POSWECHSEL_BETT_error_0" aria-invalid="true" data-clipboard="M37_Q_POSWECHSEL_BETT#B1" type="radio">
0
</label>
Here, I'd like to select the radio buttons and select them with the following code:
radios = driver.find_elements_by_xpath("//input[starts-with(#id, 'M37_Q_')][#value='B1']")
for radio in radios:
# just check the id
print(radio.get_attribute('id'))
radio.click()
It correctly selects the elements in question. However, it does not get selected nor does it yield any obvious errors. Can we use .click() to select radio buttons here? Is this some kind of handler problem?

Try this,
driver.execute_script("arguments[0].checked = true;",element)
You can also try by sending ENTER Key to the element.

Try using following Css selector:
Actions action = new Actions(drive);
action.moveToElement(drive.findElement( By.cssSelector("label > input[id^='M37_Q_']"))).build().perform();
drive.findElement( By.cssSelector("label > input[id^='M37_Q_']")).click();

Related

Check the label of a checkbox using Selenium and behave in Python

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')

Can't select an option using Selenium + Python

I can click on a field, so drop down appears, but then I am stuck
It has:
<select class="choices select optional choices__input is-hidden chzn-select foursix chzn-done" name="pack[company_billing_plan_id]" id="pack_company_billing_plan_id" tabindex="-1" style="display: none;" aria-hidden="true" data-choice="active">
<option value = "A" selected = ""> NAME </option>
where A and NAME tend to change depends on the option you choose, so they are different each time.
Any ideas on how to make the thing real and to do that?
PS. My part of the code:
self.group.choice = self.session.driver.find_element_by_xpath('/html/body/main/section[2]/div[2]/div/'
'form/fieldset[1]/dl[2]/dd/div/div[1]')
self.group.choice.click()
I beleive you are using xpath for the object identication and its getting changed as whatever is being selected there.
Try to select the nth-child of the parent object which are having non dynamic properties
by_css = [select*="choices select optional choices"] > a:nth-child(no)
no = whatever element u want to reach(1,2,3,)

selenium not setting input field value

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')

Checking for a field error using Selenium Webdriver

I've been trying to implement tests to check for field validation in forms. A check for specific field error messages was straightforward, but I've also tried a generic check to identify the parent element of a field for an error class. This however isn't working.
A field with an error has the following HTML;
<div class="field clearfix error ">
<div class="error">
<p>Please enter a value</p>
</div>
<label for="id_fromDate">
<input id="id_fromDate" type="text" value="" name="fromDate">
</div>
So to check for an error I've got the following function;
def assertValidationFail(self, field_id):
# Checks for a div.error sibling element
el = self.find(field_id)
try:
error_el = el.find_element_by_xpath('../div[#class="error"]')
except NoSuchElementException:
error_el = None
self.assertIsNotNone(error_el)
So el is the input field, but then the xpath always fails. I believed that ../ went up a level in the same way that command line navigation does - is this not the case?
Misunderstood your question earlier. You may try the following logic: find the parent div, then check if it contains class error, rather than find parent div.error and check NoSuchElementException.
Because .. is the way to go upper level, ../div means parent's children div.
// non-working code, only the logic
parent_div = el.find_element_by_xpath("..") # the parent div
self.assertTrue("error" in parent_div.get_attribute("class"))
When you're using a relative xpath (based on an existing element), it needs to start with ./ like this:
el.find_element_by_xpath('./../div[#class="error"]')
Only after the ./ can you start specifying xpath nodes etc.

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")

Categories

Resources