通过Chormedriver运行网页访问代码

目录

Chormedriver的安装方式请见

通过Chormedriver运行网页访问代码步骤

1.Python引入数据库

 2.调用selenium工具包

3.绕过selenium检测

4.Chrome.exe放大为全屏

5.登录账户

6.进行滑块移动

7.跳转下个页面 ,进行页面访问

全部代码预览


Chormedriver的安装方式请见:

Chormedriver下载和安装(2022-09-12)____March___的博客-CSDN博客

通过Chormedriver运行网页访问代码步骤:

1.Python引入数据库:

# 安装time工具包:pip install time
import time
# 安装selenium工具包:pip install selenium
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

 2.调用selenium工具包:

# 建立chromedriver.exe驱动位置
Load = Service("**********")  #其中“**********”=>Chormedriver安装位置
# 导入驱动启动绝对路径
driver = webdriver.Chrome(service=Load)
# 打开登录网址
Front_Page_url = 'https://kyfw.12306.cn/otn/resources/login.html'
driver.get(Front_Page_url)

3.绕过selenium检测:

# 绕过selenium检测
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",
                       {"source": """Object.defineProperty(navigator,'webdriver',{get:()=>undefined})"""})

4.Chrome.exe放大为全屏:

# Chrome.exe放大为全屏
driver.maximize_window()

5.登录账户:

# 输入账号和密码
user_input = driver.find_element(by=By.XPATH, value='//input[@type="text"]')
user_input.send_keys('**********') #其中“**********”=>登录账户
pw_input = driver.find_element(by=By.XPATH, value='//input[@type="password"]')
pw_input.send_keys('**********') #其中“**********”=>登录密码
# 登录
Login_btn = driver.find_element(by=By.CLASS_NAME, value='login-btn')
Login_btn.click()
time.sleep(2)

6.进行滑块移动:

# 进行滑块移动
# 标签定位滑块id
span = driver.find_element(by=By.XPATH, value='//*[@id="nc_1_n1z"]')
# 使用action操作鼠标
action = ActionChains(driver)
# 移动元素
action.move_to_element(span)
action.click_and_hold(span)
# 移动380个像素块
action.move_by_offset(380, 0)
# 放开鼠标
action.release()
# 让上面的操作执行
action.perform()
time.sleep(2)

7.跳转下个页面 ,进行页面访问:

# 关闭实名弹窗
Real_Name = driver.find_element(by=By.CLASS_NAME, value='modal-close')
Real_Name.click()
# 车票预定
Ticket_Reservation = driver.find_element(by=By.XPATH, value='//*[@id="link_for_ticket"]')
Ticket_Reservation.click()
# 关闭疫情弹窗
Epidemic_Prevention_and_Control = driver.find_element(by=By.XPATH, value='//*[@id="gb_closeDefaultWarningWindowDialog_id"]')
Epidemic_Prevention_and_Control.click()
time.sleep(1)

全部代码预览:

# 安装time工具包:pip install time
import time
# 安装selenium工具包:pip install selenium
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

# # # 进入12306官网,进行购票 # # #

# 建立chromedriver.exe驱动位置
Load = Service("**********")  #其中“**********”=>Chormedriver安装位置
# 导入驱动启动绝对路径
driver = webdriver.Chrome(service=Load)
# 打开登录网址
Front_Page_url = 'https://kyfw.12306.cn/otn/resources/login.html'
# 绕过selenium检测
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",
                       {"source": """Object.defineProperty(navigator,'webdriver',{get:()=>undefined})"""})
driver.get(Front_Page_url)
# Chrome.exe放大为全屏
driver.maximize_window()

# 输入账号和密码
user_input = driver.find_element(by=By.XPATH, value='//input[@type="text"]')
user_input.send_keys('**********') #其中“**********”=>登录账户
pw_input = driver.find_element(by=By.XPATH, value='//input[@type="password"]')
pw_input.send_keys('**********') #其中“**********”=>登录密码
# 登录
Login_btn = driver.find_element(by=By.CLASS_NAME, value='login-btn')
Login_btn.click()
time.sleep(2)

# 进行滑块移动
# 标签定位滑块id
span = driver.find_element(by=By.XPATH, value='//*[@id="nc_1_n1z"]')
# 使用action操作鼠标
action = ActionChains(driver)
# 移动元素
action.move_to_element(span)
action.click_and_hold(span)
# 移动380个像素块
action.move_by_offset(380, 0)
# 放开鼠标
action.release()
# 让上面的操作执行
action.perform()
time.sleep(2)

# # 跳转下个页面 # #
# 关闭实名弹窗
Real_Name = driver.find_element(by=By.CLASS_NAME, value='modal-close')
Real_Name.click()
# 车票预定
Ticket_Reservation = driver.find_element(by=By.XPATH, value='//*[@id="link_for_ticket"]')
Ticket_Reservation.click()
# 关闭疫情弹窗
Epidemic_Prevention_and_Control = driver.find_element(by=By.XPATH, value='//*[@id="gb_closeDefaultWarningWindowDialog_id"]')
Epidemic_Prevention_and_Control.click()
time.sleep(1)

你可能感兴趣的:(爬虫,python,chrome,开发语言)