'''
python
def ssqy(self,sbh):
time.sleep(2)
input_sbh = self.xdj_obj.find_element(By.XPATH,
'//[@id="app"]/div/div[2]/div[2]/section/div/div[3]/div[1]/form/div[1]/div/div/input')
time.sleep(1)
input_sbh.clear()
time.sleep(2)
input_sbh.send_keys(sbh)
time.sleep(2)
self.xdj_obj.find_element(By.XPATH, "//[text()='查询']").click()
time.sleep(1)
def issq(self):
try:
time.sleep(2)
zwsj = self.xdj_obj.find_element(By.XPATH, "//*[text()='暂无数据']").text
#if zwsj == '暂无数据':
print(zwsj)
isysq = 1
except:
print("有数据")
time.sleep(2)
# 点击切换按钮
self.xdj_obj.find_element(By.XPATH,"//*[@id='app']/div/div[2]/div[2]/section/div/div[3]/div[2]/div/div[3]/table/tbody/tr/td[6]/div/button").click()
time.sleep(0.5)
print("跳转到主页界面")
isysq = 0
return isysq
def fply(self):
time.sleep(5)
# 获得当前窗口
ym = self.xdj_obj.current_window_handle
# 获得全部窗口
all_ym = self.xdj_obj.window_handles
time.sleep(1)
for i in all_ym:
if i != ym:
self.xdj_obj.switch_to.window(i)
time.sleep(1.5)
# ck = self.xdj_obj.current_window_handle
time.sleep(3)
print("切换窗口成功")
time.sleep(3)
wait = WebDriverWait(self.xdj_obj, 30)
new_add = wait.until(EC.presence_of_element_located((By.XPATH,
"/html/body/div[2]/div[2]/div/div/div[1]/div/div/div[4]/div[1]/div/button/span")))
time.sleep(0.5)
new_add.click()
time.sleep(1)
def job(isysq):
uhs = []
data = r'C:\Users\Administrator\Desktop\xxx.xlsx'
with open(data, 'rb') as f:
pf = pd.read_excel(f, sheet_name='Sheet1').dropna(axis=1, how='all')
sbhs = pf.iloc[:, 1]
shs.append(sbhs)
xdjj = Xdj()
name = ''
pwd = ''
haoma = ''
xdjj.login(name, pwd, haoma)
for sh in range(len(sbhs)):
shm = pf.iloc[sh, 1]
xdjj.ssqy(shm)
xdjj.issq()
if isysq == 0:
xdjj.fplhy()
else:
xdjj.ssqy(shm)
if name == 'main':
cs = 0
job(cs)
'''
新手提问,想通过这段代码实现当“暂无数据”时循环执行xdjj.ssqy(shm),当有数据时,执行xdjj.fplhy()。但实际运行后,无论是有数据还是无数据,都会执行xdjj.fplhy()。请求指点迷津,问题出在哪里?应该怎么修改才可以实现我的想法?谢谢
根据你提供的代码,我发现问题可能出在if isysq == 0 这个判断上。在你的代码中,isysq 是由xdjj.issq()函数返回的值,并没有提供给job函数的调用参数。因此,在job函数中,isysq 始终是0,导致无论是否有数据,都执行了xdjj.fplhy()。
为了解决这个问题,你可以将isysq作为job函数的一个参数传递进去。修改job函数的定义和调用如下:
def job(isysq):
# 这里是原来的代码
#...
uhs = []
data = r'C:\Users\Administrator\Desktop\xxx.xlsx'
with open(data, 'rb') as f:
pf = pd.read_excel(f, sheet_name='Sheet1').dropna(axis=1, how='all')
sbhs = pf.iloc[:, 1]
shs.append(sbhs)
xdjj = Xdj()
name = ''
pwd = ''
haoma = ''
xdjj.login(name, pwd, haoma)
for sh in range(len(sbhs)):
shm = pf.iloc[sh, 1]
xdjj.ssqy(shm)
isysq = xdjj.issq() # 获取返回值isysq
if isysq == 0: # 根据返回值决定执行哪个方法
xdjj.fplhy()
else:
xdjj.ssqy(shm)
if name == 'main':
cs = 0
job(cs)
这样,就能在执行xdjj.issq()后获取到返回值,并根据返回值来决定后续的操作,从而实现你的想法。