菜鸟日常捯饬捣鼓记录,HW期间比较忙(lan),就写了个半自动化导包小助手
目录
1.菜鸟主要刚摸selenium的坑
1.1 selenium chrome ssl 不安全链接
1.2 selenium 定位不到元素以及恢复默认定位
1.3 selenium 的日历框输出
1.4 待添加
2.应用代码(简单注释)
1.菜鸟主要刚摸selenium的坑
1.1 selenium chrome ssl 不安全链接
path_to_chromedriver = 'C:/Path/to/Chromedriver'options = webdriver.ChromeOptions()options.add_argument('--ignore-certificate-errors')options.add_argument('--ignore-ssl-errors')browser = webdriver.Chrome(chrome_options=options, executable_path = path_to_chromedriver)参考:https://stackoverflow.com/questions/39457645/selenium-chromedriver-ssl-error-handshake-failed
1.2 selenium 定位不到元素以及恢复默认定位
[Python]
driver.switch_to_frame("mainFrame")driver.find_element_by_xpath(output_xpath).click()driver.switch_to_default_content()
switch完后,如果想要跳到其他iframe,要记得switch_to_default
1.3 selenium 的日历框输出
[Asm]
driver.find_element_by_xpath(begintime_xpath)js='document.getElementById("disabled_21_1").removeAttribute("readonly");'driver.execute_script(js)driver.find_element_by_id("disabled_21_1").clear()driver.find_element_by_xpath(begintime_xpath).send_keys("2019-06-21 06:00")
备注:将readyonly的值替换
1.4 待添加
待更新
2.应用代码(简单注释)
代码结构
2.1 代码结构
最初 是想 python调用os.system弹出来一个chrome.exe,selenium简称sele打开一个url,(遇到一个问题),输入账号密码,click点击,进入后点击日志配制管理,会出现一个导出日志管理click()但是他在iframe,点击后,弹出日历框,会有选择,别急,先把日历框的readyonly删除,然后选择把弹出框display:none,再点击导出click(),最后alert出确认吗?switch弹窗,然后刷新
为了解决selenium的ssl 不安全链接 把os.system取消,让sele自己执行,而不是在我的浏览框执行
2.3 代码简单注释
[Python]
from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsimport timeimport os,redef main(): #open chrome.exe port:9993 # open_chrome_cmd = 'chrome.exe --remote-debugging-port=9993 --user-data-dir="c:\selenum1\AutomationProfile"' # os.system(open_chrome_cmd) chrome_options = Options() # chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9993") chrome_options.add_argument('--ignore-certificate-errors') chrome_options.add_argument('--ignore-ssl-errors') chrome_driver = "C:/Users/Administrator/Desktop/chromedriver1.exe" driver = webdriver.Chrome(chrome_options=chrome_options, executable_path = chrome_driver) # driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options) url = "https://**************/login/requireLogin" username = "**************" password = "**************" time.sleep(3) driver.get(url) button_xpath = '//*[@id="loginButton"]' user_xpath = '//*[@id="username"]' pass_xpath = '//*[@id="password"]' time.sleep(1) if driver.find_element_by_xpath(button_xpath): elem=driver.find_element_by_xpath(user_xpath) elem.send_keys(username) elem=driver.find_element_by_xpath(pass_xpath) elem.send_keys(password) driver.find_element_by_xpath(button_xpath).click() print("login succses") # log_jump_xpath = '//*[@id="one2"]' log_jump2_xpath = '//*[@id="two26"]' output_xpath = '//*[@id="btn_ex_webseclog_f"]' begintime_xpath = '//*[@id="disabled_21_1"]' endtime_xpath = '//*[@id="disabled_21_2"]' startout_xpath = '//*[@id="btn_clr_all"]' # print(time.strftime('%Y-%m-%d %H:%M')) # 2019-06-20 17:24 driver.find_element_by_xpath(log_jump_xpath).click() time.sleep(1) driver.find_element_by_xpath(log_jump2_xpath).click() time.sleep(1) #clear log click clear_log_xpath = '//*[@id="btn_del_webseclog"]' driver.switch_to_frame("mainFrame") driver.find_element_by_xpath(clear_log_xpath).click() #accept alert alert=driver.switch_to.alert alert.accept() #open wevseclog page time.sleep(1) driver.switch_to_default_content() driver.switch_to_frame("mainFrame") driver.find_element_by_xpath(output_xpath).click() driver.switch_to_default_content() time.sleep(2) driver.switch_to_frame("dialogFrame") driver.find_element_by_xpath(begintime_xpath) js='document.getElementById("disabled_21_1").removeAttribute("readonly");' driver.execute_script(js) driver.find_element_by_id("disabled_21_1").clear() driver.find_element_by_xpath(begintime_xpath).send_keys("2019-06-21 06:00") time.sleep(2) driver.find_element_by_xpath(endtime_xpath) js='document.getElementById("disabled_21_2").removeAttribute("readonly");' driver.execute_script(js) driver.find_element_by_id("disabled_21_2").clear() driver.find_element_by_xpath(endtime_xpath).send_keys("2019-06-21 08:00") driver.switch_to_default_content() driver.find_element_by_xpath('//*[@id="_my97DP"]') js='document.getElementById("_my97DP").removeAttribute("style");' driver.execute_script(js) time.sleep(2) driver.switch_to_default_content() driver.switch_to_frame("dialogFrame") driver.find_element_by_xpath(startout_xpath).click() #accept alert alert=driver.switch_to.alert alert.accept() #reflash driver.refresh() while(1): passif __name__ == "__main__": main() print("end")