Can't select an option using Selenium + Python - 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,)

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

Radio button does not get clicked in Selenium / 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();

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

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.

Categories

Resources