首先我们先来做个小案例,要求如下:
代码如下:
# 相关导入
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
if __name__ == '__main__':
# 实例化浏览器对象
web = Chrome()
# 全屏
web.maximize_window()
# 使用get方法进入网站
web.get('https://www.iviewui.com')
time.sleep(1)
# 点击体验免费组件库
web.find_element(By.XPATH, '//a[@href="/view-ui-plus/"]').click()
time.sleep(1)
# 点击组件
web.find_element(By.XPATH, '//span[text()="组件"]/../..').click()
time.sleep(1)
# 点击表单
web.find_element(By.XPATH, '//span[text()="表单"]/../..').click()
time.sleep(1)
# 点击Radio
web.find_element(By.XPATH, '//a[@href="/view-ui-plus/component/form/radio"]').click()
time.sleep(1)
# 点击Windows单选框
web.find_element(By.XPATH, '//span[text()="Windows"]/preceding-sibling::span/input').click()
time.sleep(1)
# 点击checkbox
web.find_element(By.XPATH, '//a[@href="/view-ui-plus/component/form/checkbox"]').click()
time.sleep(1)
# 依次勾选上那些未勾选上的复选框
web.find_element(By.XPATH, '//span[text()="Twitter"]').click()
time.sleep(1)
web.find_element(By.XPATH, '//span[text()="Snapchat"]').click()
time.sleep(1)
web.find_element(By.XPATH, '//span[text()="香蕉"]').click()
time.sleep(1)
web.find_element(By.XPATH, '//span[text()="西瓜"]').click()
time.sleep(1)
# 停留五秒
time.sleep(5)
# 关闭浏览器,selenium4加不加close方法都会关闭浏览器
web.close()
# 相关导入
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
if __name__ == '__main__':
# 实例化浏览器对象
web = Chrome()
# 全屏
web.maximize_window()
# 使用get方法进入网站
web.get('https://sahitest.com/demo/selectTest.htm')
time.sleep(1)
# 选取select标签
my_select = Select(web.find_element(By.ID, 'testInputEvent'))
# 根据索引选取值
# my_select.select_by_index(3)
# 根据value选取值
# my_select.select_by_value('value3')
# 根据用户可见的选项内容选取值
# my_select.select_by_visible_text('Option 2')
# 停留五秒
time.sleep(5)
# 关闭浏览器,selenium4加不加close方法都会关闭浏览器
web.close()
# 相关导入
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
if __name__ == '__main__':
# 实例化浏览器对象
web = Chrome()
# 全屏
web.maximize_window()
# 使用get方法进入网站
web.get('https://sahitest.com/demo/alertTest.htm')
time.sleep(1)
# 点击按钮弹出弹窗
web.find_element(By.NAME, 'b1').click()
time.sleep(1)
# 输出弹窗内容
print(web.switch_to.alert.text)
# 点击弹窗的确认按钮
web.switch_to.alert.accept()
# 停留五秒
time.sleep(5)
# 关闭浏览器,selenium4加不加close方法都会关闭浏览器
web.close()
# 相关导入
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
if __name__ == '__main__':
# 实例化浏览器对象
web = Chrome()
# 全屏
web.maximize_window()
# 使用get方法进入网站
web.get('https://sahitest.com/demo/confirmTest.htm')
time.sleep(1)
# 点击按钮弹出弹窗
web.find_element(By.NAME, 'b1').click()
time.sleep(1)
# 输出弹窗内容
print(web.switch_to.alert.text)
# 点击弹窗的确认按钮
# web.switch_to.alert.accept()
# 点击弹窗的取消按钮
web.switch_to.alert.dismiss()
# 停留五秒
time.sleep(5)
# 关闭浏览器,selenium4加不加close方法都会关闭浏览器
web.close()
# 相关导入
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
if __name__ == '__main__':
# 实例化浏览器对象
web = Chrome()
# 全屏
web.maximize_window()
# 使用get方法进入网站
web.get('https://sahitest.com/demo/promptTest.htm')
time.sleep(1)
# 点击按钮弹出弹窗
web.find_element(By.NAME, 'b1').click()
time.sleep(1)
# 输入内容
web.switch_to.alert.send_keys('我输出了内容')
# 点击弹窗的确认按钮
web.switch_to.alert.accept()
# 点击弹窗的取消按钮
# web.switch_to.alert.dismiss()
# 停留五秒
time.sleep(5)
# 关闭浏览器,selenium4加不加close方法都会关闭浏览器
web.close()