1

I'm trying to create a script to automatically fill in timecard for my work. I'm having trouble implementing for loop with selenium though.

For example, i have these many lines:

    browser.find_element_by_id("t_z12022421023027015111a68_b_1_r1").send_keys(8)
    browser.find_element_by_id("t_z12022421023027015111a68_b_2_r1").send_keys(8)
    browser.find_element_by_id("t_z12022421023027015111a68_b_3_r1").send_keys(8)
    browser.find_element_by_id("t_z12022421023027015111a68_b_4_r1").send_keys(8)
    browser.find_element_by_id("t_z12022421023027015111a68_b_5_r1").send_keys(8)

and I would like to shorten them:

for i in range(1, 6):
    browser.find_element_by_id("t_z12022421023027015111a68_b_[i]_r1").send_keys(8)

However, I get this error message:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

Could someone please help? I have python 2.7 and use chrome as my browser.

I don't know what I'm doing wrong... Thank you in advance! :)

1

1 Answer 1

1

The [i] would not auto-magically be replaced by the value of the i variable, you should "format" it into the string instead:

for i in range(1, 6):
    browser.find_element_by_id("t_z12022421023027015111a68_b_{i}_r1".format(i=i)).send_keys(8)
Sign up to request clarification or add additional context in comments.

Comments

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.