我在页面A上有一个课程列表,在单击某个课程的编辑后,它会将我带到页面B,在那里我会执行某些操作。单击“保存并返回”时,将返回到页面A,在那里我想要单击并编辑列表中的以下项目。问题是我丢失了列表上前一项的DOM或状态(不确定如何引用它),这会导致我的循环中断,而不是转到下一项并执行必要的操作。
#iterate backup ul with each year and faculty
lists = ['2018-1','2017-1','2017-2','2016-2','2016-3']
faculties = ['FEA','FMSS','FNAHSW','FST','ODL']
for link_text in lists:
link = driver.find_element_by_link_text(link_text)
link.click()
time.sleep(1)
for facs in faculties:
link = driver.find_element_by_link_text(facs)
link.click()
#check to see if faculties have courses
if driver.find_element_by_class_name('dimmed').text != 0:
#traverse course list
resultSet = driver.find_element_by_class_name('course-list')
options = resultSet.find_elements_by_tag_name("li")
#traverse and click each edit course button
for option in options:
time.sleep(3)
option.find_element_by_class_name('action-edit').click()
#inside edit page
#convert to CAPS
Inputelement = driver.find_element_by_id('id_fullname')
time.sleep(1)
String = Inputelement.get_attribute('value')
time.sleep(1)
Inputelement.send_keys(Keys.CONTROL + "a")
Inputelement.send_keys(Keys.DELETE)
time.sleep(1)
Inputelement.send_keys(String.upper())
#save and return
driver.find_element_by_id('id_saveandreturn').click()
else:
print ('List has no courses')
time.sleep(1)发布于 2019-04-05 06:28:36
欢迎来到SO。我建议在每次迭代中使用索引,而不是尝试访问以前存储的元素。如果您是指原始帖子(OP)中的选项,则将options列表更改为,如下所示。
# Get the number of options
options = driver.find_elements_by_xpath("//*[@class='course-list']/li")
# iterate through each option
for optionCounter in range(len(options)):
# get the option here based on index
option = driver.find_element_by_xpath("(//*[@class='course-list']/li)[" + str(optionCounter) + "]")
# continue working with option from herehttps://stackoverflow.com/questions/55524340
复制相似问题