Selenium tutorial: cascade selection + date box + pop-up box, sample exercises for components

1. Cascader cascading selection usually refers to selecting among multiple levels or categories. The selection of each level or category depends on the selection result of the previous level or category. Commonly used in provinces and cities, company levels, business classification, etc.

Web page element structure
Insert image description here
implementation code

# @Author : 小红牛
# 微信公众号:WdPython
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://iviewui.com/view-ui-plus/component/form/cascader')
driver.find_element(By.XPATH, '//input[@class="ivu-input ivu-input-default"]').click()
# 找到li标签下,包含北京和故宫的关键字
driver.find_element(By.XPATH, '//li[contains(text(),"北京")]').click()
driver.find_element(By.XPATH, '//li[contains(text(),"故宫")]').click()

sleep(5)
driver.quit()

2. For the usage of date boxes, by observing the structure of the web page, you can directly use driver.find_elements to locate all date boxes based on the class attribute in the input tag. Then the subscript index [ ] takes out the required position.

Implement code

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://iviewui.com/view-ui-plus/component/form/date-picker')
# 1.定位元素
date_input = driver.find_elements(By.XPATH, '//input[@class="ivu-input ivu-input-default ivu-input-with-suffix"]')
date_input[0].click()
# 2.输入一个时间
date_input[0].send_keys('2023-12-31')
sleep(2)
# 3.输入时间范围
date_input[1].click()
date_input[1].send_keys('2023-12-01 - 2023-12-31')
sleep(5)
driver.quit()

3. Example usage of pop-up boxes, 1. Why should we deal with pop-up boxes? Once a pop-up box appears, if it is not processed, subsequent operations will not be possible.
3.2. Pop-up window classification: System pop-up window: JS implementation, custom pop-up window: front-end code encapsulation.
3.3. Classification of dialog boxes: alert: warning box + confirm: confirmation box + prompt: prompt box.

3.4. How to handle the pop-up window with only OK button
Insert image description here

# @Author : 小红牛
# 微信公众号:WdPython
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://sahitest.com/demo/alertTest.htm')
driver.find_element(By.NAME, 'b1').click()
# 显示弹框上的文字内容
print(driver.switch_to.alert.text)
# Alert Message
sleep(2)
# 点击确定
driver.switch_to.alert.accept()
sleep(5)
driver.quit()

3.5. How to handle pop-up windows with cancel + confirm buttons
Insert image description here

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://sahitest.com/demo/confirmTest.htm')
driver.find_element(By.NAME, 'b1').click()
# 1.显示弹框上的文字内容
print(driver.switch_to.alert.text)
# Alert Message
sleep(2)
# 2.点击确定
# driver.switch_to.alert.accept()
# 3.点击取消
driver.switch_to.alert.dismiss()
sleep(5)
driver.quit()

3.6. The pop-up window is in the style of an input box. Enter the content first and then click the OK button.
Insert image description here

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://sahitest.com/demo/promptTest.htm')
driver.find_element(By.NAME, 'b1').click()
# 1.显示弹框上的文字内容
print(driver.switch_to.alert.text)
# Some prompt?
sleep(2)
# 2.输入内容
driver.switch_to.alert.send_keys('我是李白')
sleep(2)
# 3.点击确定
driver.switch_to.alert.accept()
sleep(5)
driver.quit()

complete! ! thanks for watching

----------★★Historical blog post collection★★----------
My zero-based Python tutorial, Python introduction to advanced video tutorial Py installation py project Python module Python crawler Json
Insert image description here

Guess you like

Origin blog.csdn.net/gxz888/article/details/135452258