经过前几周的加班,每天都要提一遍加班申请,感觉很繁琐,想到写一个自动提交加班申请的定时任务。然后写完自动提交加班申请这一部分,感觉有几个鲜明的点,所以贴过来记录一下。由于是公司内部系统,所以链接无法访问,所以地址和用户都用“XXX”代替!
from selenium import webdriverdriver=webdriver.Chrome()driver.get("http://xxxxxxx/")driver.maximize_window()driver.find_element_by_css_selector('[name="UserID"]').send_keys(xxx)driver.find_element_by_css_selector('[name="Passwrd"]').send_keys('xxxx')driver.find_element_by_css_selector('[value="登录"]').click()driver.find_element_by_css_selector('[href="javascript:portal()"]').click()frameElement = driver.find_element_by_css_selector('#main')driver.switch_to.frame(frameElement)#iframe跳转import timetime.sleep(5)js = "document.getElementById('sidebar_L').style.width = '300px';document.getElementById('sidebar_L').style.heigth = '300px';"driver.execute_script(js)#div的高,宽为0,需要修改后才能获取到下面的元素time.sleep(5)driver.find_element_by_xpath('//div[@id="sidebar_L"]/div[@class="mokuai15 rsg5"]').click()#忽然看到了xpath获取定位的便利time.sleep(5)driver.find_element_by_xpath('/html/body/form/table/tbody/tr[5]/td[2]/input').click()time.sleep(5)frameElement1=driver.find_element_by_xpath('/html/body/div/iframe')driver.switch_to.frame(frameElement1)#又需要跳转到一个iframe,这个iframe没有id,没有name,所以用了xpath方式定位driver.find_element_by_xpath('//*[@id="dpTodayInput"]').click()driver.switch_to.parent_frame()#需要再跳回到上一个iframetime.sleep(5)js="document.getElementById('st_gl0').parentElement.children[13].children[3].children[0].children[7].children[1].children[0].value='19:30'"driver.execute_script(js)#input框内的默认值需要先删除再修改,所以直接用js给修改了。time.sleep(5)js="document.getElementById('st_gl0').parentElement.children[13].children[3].children[0].children[8].children[1].children[0].value='21:00'"driver.execute_script(js)time.sleep(5)js="document.getElementById('st_gl0').parentElement.children[13].children[3].children[0].children[9].children[1].children[0].value='1.5'"driver.execute_script(js)time.sleep(5)driver.find_element_by_xpath('/html/body/form/table/tbody/tr[16]/td[2]/select/option[2]').click()#select方式可以用css定位,但是xpath比较方便,就用了xpathtime.sleep(5)driver.find_element_by_xpath('/html/body/form/table/tbody/tr[25]/td[2]/input').click()driver.switch_to.alert.accept()#对弹窗alert的处理 time.sleep(5) driver.quit()
自动充值例子:
from selenium import webdriverdriver = webdriver.Chrome()driver.get('http://XXXXXX')driver.maximize_window()driver.find_element_by_css_selector('[name="name"]').send_keys('XXXXX')driver.find_element_by_css_selector('[name="passwd"]').send_keys('XXXX')driver.find_element_by_css_selector('[οnclick="check_login()"]').click()driver.find_element_by_css_selector('span[style="line-height:100%;"]').click()driver.find_element_by_css_selector('[href="/?system_id=3"]').click()driver.find_element_by_css_selector('[href="/tree?parent_id=1006"]').click()js = "document.getElementById('leftside').style.width = '500px';document.getElementById('leftside').style.heigth = '500px';"driver.execute_script(js)import timetime.sleep(1)driver.find_element_by_css_selector('.cur>div>a[rel="rbplist"]').click()from selenium.webdriver.support.select import Selectimport timetime.sleep(1)s=driver.find_element_by_css_selector("#recharge_way")Select(s).select_by_index(1)driver.find_element_by_css_selector('[name="usr"]').send_keys("r0XXXX")#充值账号driver.find_element_by_css_selector('[name="amount"]').send_keys(10)driver.find_element_by_css_selector('#reason').send_keys("test")driver.find_element_by_css_selector('.buttonContent>button[name="submit"]').click()from selenium.webdriver.common.alert import Alertimport timetime.sleep(5)driver.find_element_by_css_selector('[rel="callback"]').click()