以下分别给出两组不同组合类型,组合都是状态+上传 第一组只有div标签页,获取到的xpath也是div组成,规则不明显 第二组span标签页,获取到的xpath以tr[1],tr[2]...tr[n]规则 页面元素组成类型: 待上传 已上传 上传元素对应: 上传 上传元素对应xpath: //*[@id="app"]/div[1]/div[2]/div/div/div[1]/div[2]/div[8]/div/div[3] //*[@id="app"]/div[1]/div[2]/div/div/div[1]/div[2]/div[9]/div/div[3] 页面元素组成类型: 待上传 已上传 上传元素对应: 上传元素对应xpath: //*[@id="app"]/div[1]/div[2]/div/div/main/div/div/div[3]/table/tbody/tr[1]/td[5]/div/div/div/div/button //*[@id="app"]/div[1]/div[2]/div/div/main/div/div/div[3]/table/tbody/tr[2]/td[5]/div/div/div/div/button
get_attribute
,以get_attribute
获取到的属性为筛选条件*新增一个标志变量*uploaded_found*,初始化为 False
,当状态元素标志都变为已上传状态时*uploaded_found
=True并且停止循环第一组以div标签的具体代码:
#上传按钮元素 documentfile = "//div[@class='size'][@data-v-763e4ba7]" #未上传状态元素 upload = "//div[@class='main yellow'][@data-v-763e4ba7]" #未上传总数 uploads = self.driver.find_elements(By.XPATH, upload) #上传按钮总数 documents = self.driver.find_elements(By.XPATH, documentfile) while True: if len(uploads) > 1: for i in range(0, len(uploads)): #某一行只存在未上传且不存在已上传状态时达成执行条件 if "main yellow" in uploads[i].get_attribute("class") and "main blue" not in uploads[i].get_attribute("class"): #开始循环执行上传附件操作 self.driver.execute_script("arguments[0].click();", documents[i]) #上传文件的具体地址 self.updowm.PyautoguiWrite(file_path="C:\\Users\\GTJA\\Pictures\\testfilepdf") #加上等待时间 time.sleep(2) #判断文件是否全部上传,若全部上传则len(uploadcounts) == len(uploads)成立 uploadcount = "//div[@class='main blue'][@data-v-763e4ba7]" uploadcounts = self.driver.find_elements(By.XPATH, uploadcount) if len(uploadcounts) == len(uploads): break
第二组以tr为标签的具体代码:
self.driver.execute_script("window.scrollTo(0, 0);") #上传按钮元素 documentfile = '//*[@id="app"]/div[1]/div[2]/div/main/div/main/form/div/div/div/div[1]/div[4]/div[2]/table/tbody/tr/td[3]/div/div/div/div/button' #状态元素 upload = '//*[@id="app"]/div[1]/div[2]/div/main/div/main/form/div/div/div/div[1]/div[3]/table/tbody/tr/td[2]/div/div' #获取到所有状态元素个数 uploads = self.driver.find_elements(By.XPATH, upload) #获取到所有上传按钮个数 documents = self.driver.find_elements(By.XPATH, documentfile) #新增一个标志变量,初始化为False uploaded_found = False while True: if len(uploads) > 1: for i in range(0, len(uploads)): #状态未未上传且已上传不存在则条件达成,开始循环 if "container pending" in uploads[i].get_attribute("class") and "container pending uploaded" not in uploads[ i].get_attribute("class"): #循环上传文件 self.driver.execute_script("arguments[0].click();", documents[i]) #上传文件地址 self.updowm.PyautoguiWrite(file_path="C:\\Users\\GTJA\\Pictures\\testfilepdf") time.sleep(2) #状态都变为已上传状态,将标志变量设置为True if uploads[i].get_attribute("class") == "container pending uploaded": uploaded_found = True # 当条件达成时,设置标志变量为 True break if uploaded_found: # 根据标志变量的值来判断是否结束外层的 while 循环 break