12306查询测试:https://www.12306.cn/index/

from selenium import webdriver
from time import sleep
# 谷歌浏览器驱动
path = r"E:\software\python_Related\chromedriver.exe"

driver = webdriver.Chrome(executable_path=path)

driver.get("https://www.12306.cn/index/")


from selenium.webdriver.common.keys import Keys

# 通过 js代码 移除 只读属性
#  document.getElementById('train_date').removeAttribute('readonly')
# driver.execute_script("document.getElementById('train_date').removeAttribute('readonly')")
sleep(4)
#readonly 属性 只能读取

search = driver.find_element_by_id("search_one")

tran = driver.find_element_by_id("train_date")
# arguments[0].removeAttribute('readonly');
driver.execute_script("arguments[0].removeAttribute('readonly');",tran)
driver.find_element_by_id("train_date").clear()
driver.find_element_by_id("train_date").send_keys("2020-08-10")

driver.find_element_by_id("fromStationText").send_keys("北")
driver.find_element_by_id("fromStationText").send_keys(Keys.ENTER)
sleep(1)
driver.find_element_by_id("fromStationText").send_keys(Keys.TAB)
driver.find_element_by_id("toStationText").send_keys("上")
driver.find_element_by_id("toStationText").send_keys(Keys.ENTER)
sleep(1)

# search.click()
#模拟鼠标
from selenium.webdriver.common.action_chains import ActionChains

# 根据元素偏移量定位
# ActionChains(driver).move_by_offset(160,450).perform()
ActionChains(driver).move_to_element_with_offset(search,10,10).perform()
ActionChains(driver).click().perform()



抱歉,作为AI模型,我无法提供屏幕截图和代码。但是,我可以提供一些代码示例和步骤,帮助你进行alert对话框处理方法的测试。 步骤1:导入selenium库 ```python from selenium import webdriver ``` 步骤2:启动浏览器并访问测试网站 ```python driver = webdriver.Chrome() # 启动Chrome浏览器 driver.get("https://www.12306.cn/index/") # 访问测试网站 ``` 步骤3:使用强制等待方法等待页面加载完成 ```python import time time.sleep(5) # 强制等待5秒钟 ``` 步骤4:使用隐式等待方法等待元素加载完成 ```python driver.implicitly_wait(10) # 隐式等待10秒钟 ``` 步骤5:使用三种元素定位方法之一定位弹出警告对话框 ```python # 使用ID定位 alert_button = driver.find_element_by_id("alert_button_id") # 使用NAME定位 alert_button = driver.find_element_by_name("alert_button_name") # 使用XPATH定位 alert_button = driver.find_element_by_xpath("//button[contains(text(), '弹出警告对话框')]") ``` 步骤6:点击弹出警告对话框按钮并处理alert对话框 ```python alert_button.click() # 点击弹出警告对话框按钮 alert = driver.switch_to.alert # 切换到alert对话框 alert_text = alert.text # 获取alert对话框文字 print(alert_text) # 打印alert对话框文字 alert.accept() # 关闭alert对话框 ``` 步骤7:关闭浏览器 ```python driver.quit() # 关闭浏览器 ``` 完整的测试代码示例: ```python from selenium import webdriver import time driver = webdriver.Chrome() # 启动Chrome浏览器 driver.get("https://www.12306.cn/index/") # 访问测试网站 time.sleep(5) # 强制等待5秒钟 driver.implicitly_wait(10) # 隐式等待10秒钟 # 使用ID定位 alert_button = driver.find_element_by_id("alert_button_id") # 使用NAME定位 alert_button = driver.find_element_by_name("alert_button_name") # 使用XPATH定位 alert_button = driver.find_element_by_xpath("//button[contains(text(), '弹出警告对话框')]") alert_button.click() # 点击弹出警告对话框按钮 alert = driver.switch_to.alert # 切换到alert对话框 alert_text = alert.text # 获取alert对话框文字 print(alert_text) # 打印alert对话框文字 alert.accept() # 关闭alert对话框 driver.quit() # 关闭浏览器 ``` 注意:在测试过程中,为了保护个人隐私,建议使用虚拟账号进行测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雲小妖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值