selenium操作下拉选和网页提示框

时间:2023-12-24 08:30:13
import time
from selenium import webdriver
from selenium.webdriver.support.select import Select#处理下拉框
from selenium.webdriver.support.ui import WebDriverWait#等待一个元素加载完成
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys#键盘
from selenium.webdriver.common.action_chains import ActionChains#鼠标 driver= webdriver.Chrome()
driver.get("http://www.baidu.com")
time.sleep(3) #找到设置,并选择搜索设置选项
settings = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(settings).perform()#鼠标停留
time.sleep(3) driver.find_element_by_link_text("搜索设置").click()
time.sleep(3) #操作,设置每页显示50条
select = driver.find_element_by_id("nr")
Select(select).select_by_index(2)
time.sleep(2) driver.find_element_by_class_name("prefpanelgo").click()#点击保存按钮,弹出提示框
time.sleep(5)
driver.switch_to.alert.accept()#接受弹窗 driver.quit() #提取网页上所有能看见的文本内容
#能在网页上看见的文本都在标签之间,不可能在标签内
tags = driver.find_elements_by_xpath("/*")
for tag in tags:
print(tag.text)