Issue
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! :)
Solution
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)
Answered By - alecxe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.