import os import time from selenium import webdriver from selenium.webdriver import ActionChains, Keys from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC def wait_xpath(ChromeDriver, xpath_c, wait_time=20): # WebDriverWait(ChromeDriver, wait_time, 0.5).until(EC.presence_of_element_located((By.XPATH, xpath_c))) WebDriverWait(ChromeDriver, wait_time, 0.5).until(EC.visibility_of_element_located((By.XPATH, xpath_c))) WebDriverWait(ChromeDriver, wait_time, 0.5).until(EC.element_to_be_clickable((By.XPATH, xpath_c))) ele = ChromeDriver.find_element(By.XPATH, xpath_c) print(ele) return ele current_directory = os.getcwd() print("当前运行目录:", current_directory) chrome_location = current_directory+'\\chrome-win64\\chrome.exe' chromedriver_path = current_directory+'\\driver\\chromedriver.exe' opt = Options() opt.add_argument('start-maximized') opt.binary_location = chrome_location driver = webdriver.Chrome(options=opt, service=Service(chromedriver_path)) driver.get('http://202.127.42.188/rawmilkstation/index.jsp') while True: try: time.sleep(1) driver.find_element(By.XPATH, '//*[@id="component-1011"]/div/ul/li[2]/a') break except Exception as e: continue wait_xpath(driver, '//*[@id="component-1011"]/div/ul/li[2]/a/span').click() wait_xpath(driver, '//*[@id="component-1011"]/div/ul/li[2]/ul/li[5]/a/span').click() from main import getData3 data = getData3() for k, v in data.items(): print(k, v) while True: try: ele = wait_xpath(driver, '/html/body/div[4]/div[2]/div[2]/div[2]/div/div/div[3]/div') ActionChains(driver).move_to_element(ele).perform() wait_xpath(driver, '/html/body/div[4]/div[2]/div[2]/div[2]/div/div/div[3]/div/div').click() wait_xpath(driver, '/html/body/div[14]/div/div[2]/div[2]/div[4]/a').click() ele = wait_xpath(driver, '/html/body/div[15]/div/div[2]/div/table/tbody/tr/td[2]/input') # ele.clear() ele.send_keys(Keys.BACKSPACE * 20) ele.send_keys(k) ele.send_keys(Keys.RETURN) time.sleep(5) break except Exception as e: print(e) driver.get('http://www.moarm.cn/index.jsp') while True: try: time.sleep(1) driver.find_element(By.XPATH, '//*[@id="component-1011"]/div/ul/li[2]/a') break except Exception as e: continue wait_xpath(driver, '//*[@id="component-1011"]/div/ul/li[2]/a/span').click() wait_xpath(driver, '//*[@id="component-1011"]/div/ul/li[2]/ul/li[5]/a/span').click() while True: try: if len(wait_xpath(driver, '/html/body/div[4]/div[2]/div[2]/div[3]/div/table').find_elements(By.TAG_NAME, "tr")) == 1: break except Exception as e: print(e) continue wait_xpath(driver, '/html/body/div[4]/div[2]/div[2]/div[3]/div/table/tbody/tr/td[2]/div/img').click() # wait_xpath(driver,'/html/body/div[12]/div[2]/div/div/div/div[2]/div[1]/div/span/div/table[4]/tbody/tr/td[2]/span/div/div/div[2]/div[1]/div/span/div/div[2]/div[3]/div/table/tbody/tr[1]/td[3]') # table = wait_xpath(driver,'') print('aaaaaaa') iframes = driver.find_elements(By.TAG_NAME, 'iframe') print(iframes) # driver.switch_to.frame(iframes[0]) time.sleep(4) while True: try: sampleTestItemGrid = driver.find_element(By.ID, 'sampleTestItemGrid-body') table = sampleTestItemGrid.find_element(By.TAG_NAME, 'table') print('table = ', table) rows = table.find_elements(By.TAG_NAME, "tr") for row in rows: # 获取每行的单元格 cells = row.find_elements(By.TAG_NAME, "td") # for cell in cells: # print(cell.text) if cells: print(cells) print(cells[0].text) print(cells[1].text) print(cells[2].text) print(cells[3].text) print(cells[4].text) print(cells[5].text) # ActionChains(driver).move_to_element(cells[5]).click(cells[5]).perform() driver.execute_script("arguments[0].click();", cells[7]) # time.sleep(1) ele = wait_xpath(driver, '//input[@name="faTestMethod"]', wait_time=5) print(ele) # ele.clear() ele.send_keys(Keys.BACKSPACE * 70) if cells[2].text in v.keys(): stdNo = v[cells[2].text] if '快速' in stdNo: ele.send_keys(stdNo) e1 = wait_xpath(driver, "//li[contains(text(),'{}')]".format(stdNo)) driver.execute_script("arguments[0].click();", e1) else: ele.send_keys('[{}]'.format(stdNo)) e1 = wait_xpath(driver, "//li[contains(text(),'[{}]')]".format(stdNo)) driver.execute_script("arguments[0].click();", e1) ele.send_keys(Keys.RETURN) # time.sleep(2) print('ok') break except Exception as e: print(e) continue wait_xpath(driver, '//*[@id="saveBtn-btnInnerEl"]').click()