1.alert弹框定位

from selenium import webdriver
from selenium.webdriver.common.by import By
import time  # 添加时间模块

driver = webdriver.Chrome()
driver.get("https://sahitest.com/demo/alertTest.htm")
driver.find_element(By.NAME, 'b1').click()
time.sleep(2)
#使用alert.text获取弹框的文字
print(driver.switch_to.alert.text)
#点击确定
driver.switch_to.alert.accept()
time.sleep(2)

2.confirm定位(确定)

from selenium import webdriver
from selenium.webdriver.common.by import By
import time  # 添加时间模块

driver = webdriver.Chrome()
driver.get("https://sahitest.com/demo/confirmTest.htm")
driver.find_element(By.NAME, 'b1').click()
time.sleep(2)
#使用alert.text获取弹框的文字
print(driver.switch_to.alert.text)
#点击确定
driver.switch_to.alert.accept()
#点击取消
#driver.switch_to.alert.dismiss()
time.sleep(2)

3.confirm定位(取消)

from selenium import webdriver
from selenium.webdriver.common.by import By
import time  # 添加时间模块

driver = webdriver.Chrome()
driver.get("https://sahitest.com/demo/confirmTest.htm")
driver.find_element(By.NAME, 'b1').click()
time.sleep(2)
#使用alert.text获取弹框的文字
print(driver.switch_to.alert.text)
#点击确定
#driver.switch_to.alert.accept()
#点击取消
driver.switch_to.alert.dismiss()
time.sleep(2)

4.prompt定位

from selenium import webdriver
from selenium.webdriver.common.by import By
import time  # 添加时间模块

driver = webdriver.Chrome()
driver.get("https://sahitest.com/demo/promptTest.htm")
driver.find_element(By.NAME, 'b1').click()
time.sleep(2)
#使用alert.text获取弹框的文字
print(driver.switch_to.alert.text)
#输入文字
driver.switch_to.alert.send_keys("上班啦,牛马")
time.sleep(5)
#点击确定
driver.switch_to.alert.accept()
#点击取消
#driver.switch_to.alert.dismiss()
time.sleep(2)
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐