1
<input id="value(Medications1)_139038" class="medMedications text-field" type="text" style="width:90%" value="Lantus" maxlength="30000" name="value(Medications1)" autocomplete="off"/>

So there is this page where there are textboxes and these textboxes are not fixed in number. other times it could be 20 and sometimes just 5 it's random. So is there a way to know how many of these textboxes are in the page? and get the values of those textboxes?

I've tried this:

medicationstextbox_locator = "//*[@class='medMedications text-field ']"
medicationstextbox_locatorTextBox = driver.find_elements_by_xpath(medicationstextbox_locator)

for i in medicationstextbox_locatorTextBox:
    print(i.get_attribute("values"))

this prints out a lot of None.

I don't get it caused I've accessed it's class, pulled the value but it returns None.

If it's possible I'd want to avoid doing:

medications1 = "//input[@name='value(Medications1)']"
medications2 = "//input[@name='value(Medications2)']"

med1textbox = driver.find_element_by_xpath(med1)
med2textbox = driver.find_element_by_xpath(med2)

med1textbox.get_attribute("value")
med2textbox.get_attribute("value")

because I'd have to define each textbox and the number of textboxes is not known beforehand. It would also take so much time to do it this way. So, how do we solve this?

1 Answer 1

1

print(i.get_attribute("values"))

There is no values attribute, hence, you are getting Nones. There is value attribute.

Aside from that, your first approach totally makes sense and should work.

We can make it a bit simpler and use a "CSS selector" via find_elements_by_css_selector():

medications = driver.find_elements_by_css_selector("input.medMedications")

# count
print len(medications)

# values
for medication in medications:
    print medication.get_attribute("value")

Alternatively, you can check whether an id attribute contains Medications:

medications = driver.find_elements_by_css_selector("input[id*=Medications]")

or, in case of XPath:

medications = driver.find_elements_by_xpath("//input[contains(@id, 'Medications']")
Sign up to request clarification or add additional context in comments.

2 Comments

that's all I missed? a single "s"? wth hahahahaha. thank you it worked. I can't believe I've been tearing my hear out for hours because of one "s" lol thank you
@HalcyonAbrahamRamirez it happens. Most of the times, the one would solve it during asking (the "tell your problem to the rubber duck" case). :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.