用户:
起床喵喵喵神啊查看:0 回复:1 评论:0 创建时间:2022-06-30T08:42:51
#编写python脚本vimchrome_seleuim.py
#执行命令/opt/python39/bin/python3/opt/chrome_seleuim.py #!/opt/python39/bin/python3
#encoding=utf-8importtimefromseleniumimportwebdriver
#创建chrome参数对象options=webdriver.ChromeOptions()
#解决DevToolsActivePort文件不存在的报错options.add_argument('--no-sandbox')
#指定浏览器分辨率options.add_argument('window-size=1600x900')
#谷歌文档提到需要加上这个属性来规避bugoptions.add_argument('--disable-gpu')
#隐藏滚动条,应对一些特殊页面options.add_argument('--hide-scrollbars')
#不加载图片,提升速度options.add_argument('blink-settings=imagesEnabled=false')
#浏览器不提供可视化页面.linux下如果系统不支持可视化不加这条会启动失败options.add_argument('--headless')
#初始化一个driver,chromedriver的路径选择的是相对路径,不行就写绝对路径driver=webdriver.Chrome(options=options,executable_path='./chromedriver')
#网页主页面请求路径url="喵喵喵"#请求网页driver.get(url)#通过page_source获取网页源代码,定位到输入框,并且输入登录账号信息,点击登录login_username=driver.find_element_by_id('login_username')login_username.send_keys('admin')login_password=driver.find_element_by_id('login_password')login_password.send_keys('123456')login_button=driver.find_element_by_id('login_button')login_button.click()#time.sleep(2)
#打开需要填写信息的表单页面url2='喵喵喵'driver.get(url2)time.sleep(1)
#设置表单数据提交信息#本网页使用了iframe标签,必须要使用switch_to.frame()方法进行切换,不然页面元素无法定位driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@id="zwIframe"]'))
#定位表单元素并且填写数据driver.find_element_by_xpath('/html/body/div/section/div[3]/div/section/div[2]/table/tr[4]/td[3]/div/div/section/div[2]/div/input').send_keys(time.strftime("%Y-%m-%d%H:%M:%S",time.localtime()))time.sleep(3)
#切换到上层,点击发送driver.switch_to.parent_frame()driver.find_element_by_id("sendId_a").click()time.sleep(1)
#退出浏览器driver.close()driver.quit()
此代码仅供参考,不喜勿喷
起床喵喵喵神啊#编写python脚本vimchrome_seleuim.py
#执行命令/opt/python39/bin/python3/opt/chrome_seleuim.py #!/opt/python39/bin/python3
#encoding=utf-8importtimefromseleniumimportwebdriver
#创建chrome参数对象options=webdriver.ChromeOptions()
#解决DevToolsActivePort文件不存在的报错options.add_argument('--no-sandbox')
#指定浏览器分辨率options.add_argument('window-size=1600x900')
#谷歌文档提到需要加上这个属性来规避bugoptions.add_argument('--disable-gpu')
#隐藏滚动条,应对一些特殊页面options.add_argument('--hide-scrollbars')
#不加载图片,提升速度options.add_argument('blink-settings=imagesEnabled=false')
#浏览器不提供可视化页面.linux下如果系统不支持可视化不加这条会启动失败options.add_argument('--headless')
#初始化一个driver,chromedriver的路径选择的是相对路径,不行就写绝对路径driver=webdriver.Chrome(options=options,executable_path='./chromedriver')
#网页主页面请求路径url="喵喵喵"#请求网页driver.get(url)#通过page_source获取网页源代码,定位到输入框,并且输入登录账号信息,点击登录login_username=driver.find_element_by_id('login_username')login_username.send_keys('admin')login_password=driver.find_element_by_id('login_password')login_password.send_keys('123456')login_button=driver.find_element_by_id('login_button')login_button.click()#time.sleep(2)
#打开需要填写信息的表单页面url2='喵喵喵'driver.get(url2)time.sleep(1)
#设置表单数据提交信息#本网页使用了iframe标签,必须要使用switch_to.frame()方法进行切换,不然页面元素无法定位driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@id="zwIframe"]'))
#定位表单元素并且填写数据driver.find_element_by_xpath('/html/body/div/section/div[3]/div/section/div[2]/table/tr[4]/td[3]/div/div/section/div[2]/div/input').send_keys(time.strftime("%Y-%m-%d%H:%M:%S",time.localtime()))time.sleep(3)
#切换到上层,点击发送driver.switch_to.parent_frame()driver.find_element_by_id("sendId_a").click()time.sleep(1)
#退出浏览器driver.close()driver.quit()
点赞0
评论