python selenium获取页面元素
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium import webdriver
# 参数配置
userName="xxxxxx"
password="xxxxxx"
url="xxxxxx"
loginUlr=url+"login"
indexUrl=url+"Plan/Index"
appUrl=url+"Platform/Sys/Application"
# 使用Chrome浏览器,需要安装对应版本的ChromeDriver
driver = webdriver.Chrome()
# 设置等待20秒
wait = WebDriverWait(driver, 20)
# 打开登录页面,模拟登录
driver.get(loginUlr)
username_input = driver.find_element(By.CSS_SELECTOR, "input[type='text']")
password_input = driver.find_element(By.CSS_SELECTOR, "input[type='password']")
button = driver.find_element(By.CSS_SELECTOR, "button[type='button']")
username_input.send_keys(userName)
password_input.send_keys(password)
button.click()
# 登录完成,等待跳转首页
try:
wait.until(EC.url_to_be(indexUrl))
print("登录跳转成功")
except TimeoutException:
print("登录跳转超时")
driver.quit()
exit()
# 跳转到应用页面,等待跳转到应用页面
driver.get(appUrl)
try:
wait.until(EC.url_to_be(appUrl))
print("应用跳转成功")
except TimeoutException:
print("应用跳转超时")
try:
# 等待tr加载完成后循环获取
rows = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".el-table__body tbody tr")))
for row in rows:
appName = row.find_elements(By.TAG_NAME, "td")[2];
print(appName.text)
except TimeoutException:
print("获取内容失败")
# 关闭浏览器
driver.quit()