小能豆

python逻辑判断不清晰

python

’‘’
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()。


阅读 42

收藏
2024-06-11

共1个答案

小能豆

要解决这个问题,我们需要确保 issq 方法返回的 isysq 值被正确地用于控制 job 函数中的逻辑。另外,应该注意到 Python 代码中的缩进和逻辑控制符的使用。为了确保逻辑正确,特别是在检查 isysq 值时,应该仔细检查相关代码部分的执行顺序和条件判断。

以下是修正后的代码:

import time
import pandas as pd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Xdj:
    def login(self, name, pwd, haoma):
        # 登录方法的实现
        pass

    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
            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)
        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():
    data = r'C:\Users\Administrator\Desktop\xxx.xlsx'
    pf = pd.read_excel(data, sheet_name='Sheet1').dropna(axis=1, how='all')
    sbhs = pf.iloc[:, 1]

    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()
        if isysq == 0:
            xdjj.fply()
        else:
            xdjj.ssqy(shm)

if __name__ == '__main__':
    job()

主要修改点:

  1. 确保缩进正确:Python 对缩进非常敏感,代码块必须正确缩进。
  2. isysq 赋值issq 方法的返回值:在每次调用 issq 方法后,将其返回值赋给 isysq,并根据该值决定接下来的操作。
  3. job 函数中去掉不必要的参数:删除了job 函数中的参数,改为在函数内部获取 isysq 的值。
  4. 修复 XPATH 错误:确保 XPATH 表达式的正确性,避免出现 XPATH 解析错误。

这样,在执行代码时,如果数据为“暂无数据”,将会循环执行 xdjj.ssqy(shm),而当有数据时,将执行 xdjj.fply()

2024-06-11