Loading

DATA

Python selenium库 自动化控制 实战练习
代码如下:import time from selenium import webdriver from sele...
扫描右侧二维码阅读全文
28
2021/09

Python selenium库 自动化控制 实战练习

代码如下:

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC  #显式等待
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait #显式等待
from selenium.webdriver.common.action_chains import ActionChains #鼠标操作
from selenium.webdriver.common.keys import Keys #键盘操作

'''无界面浏览器,添加浏览器参数隐藏界面
option=webdriver.ChromeOptions() # 创建浏览器参数对象
option.add_argument('--headless') # 设置option 浏览器参数
option.add_argument('--disable-gpu')
option.add_argument('window-size=1920x3000') #指定浏览器分辨率
option.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
option.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
option.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
option.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
option.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" #手动指定使用的浏览器位置
driver = webdriver.Chrome(options=option)  # 调用带参数的谷歌浏览器
driver.get("http://www.baidu.com/")
'''
option=webdriver.ChromeOptions()
option.add_argument('--headless') # 设置option 浏览器参数
option.add_argument('--disable-gpu')
browser = webdriver.Chrome(options=option)
browser.get('http://121.89.242.105:8080/#/?code=28')
name_xpash = browser.find_element_by_xpath('//*[@id="app"]/form/div[1]/div[2]/div/input')
name_xpash.send_keys('姓名')
sex_xpash = browser.find_element_by_xpath('//*[@id="app"]/form/div[2]/div[2]/div/div/div/div[1]/div')
sex_xpash.click()
tel_xpash = browser.find_element_by_xpath('//*[@id="app"]/form/div[3]/div[2]/div/input')
tel_xpash.send_keys("手机号")
store_xpash = browser.find_element_by_xpath('//*[@id="app"]/form/div[4]/div[2]/div/input')
store_xpash.click()
#点击之后此处一定要等待!!!等待页面加载,不然就会报错,找不到元素
#显式等待
# wait = WebDriverWait(browser, 30)
# wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="app"]/div[4]/div/div[1]/button[2]')))
queding_xpash = browser.find_element_by_xpath('//*[@id="app"]/div[4]/div/div[1]/button[2]')
#强制等待
# time.sleep(1)
#隐式等待
browser.implicitly_wait(5)
queding_xpash.click()
# print(browser.page_source)  #打印源码
idcard_xpash = browser.find_element_by_xpath('//*[@id="app"]/form/div[6]/div[2]/div/input')
idcard_xpash.send_keys("身份证号")
address_xpash = browser.find_element_by_xpath('//*[@id="app"]/form/div[7]/div[2]/div/input')
address_xpash.send_keys('地址')
qiye_xpash = browser.find_element_by_xpath('//*[@id="app"]/form/div[8]/div[2]/div/input')
qiye_xpash.send_keys("无")
fuwu_xpash = browser.find_element_by_xpath('//*[@id="app"]/form/div[9]/div/div/div/div')
fuwu_xpash.click()
browser.implicitly_wait(10)

yes = browser.find_elements_by_class_name('btnBox')[0]
yes.click()
browser.implicitly_wait(10)
post = browser.find_element_by_xpath('//*[@id="app"]/form/div[11]/button')
post.click()
time.sleep(0.5)
browser.save_screenshot("1.png")#截屏保存











 
最后修改:2021 年 09 月 28 日 04 : 24 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论