浏览器自动化测试工具Selenium的安装与试用

稍微复杂点,打开测试网页,登陆并查看用户信息:

from time import sleep
from selenium import webdriver
from selenium.webdriver.support.relative_locator import locate_with
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait

# 将驱动程序和当前脚本放在同一个文件夹
driver = webdriver.Chrome()
# 隐式等待,全局设置,适用于整个会话的每个元素位置调用。 默认值为0,表示如果没有找到该元素,将立即返回错误。
# 设置了隐式等待,则驱动程序将在返回错误之前等待提供的值的持续时间
# 一旦找到元素,驱动程序将返回元素引用,并且代码将继续执行
driver.implicitly_wait(2)

driver.set_window_size(1200, 675)

# 打开指定路径的页面
driver.get('https://yoursite-example.com/login')

# 登陆
driver.find_element("xpath", "//input[@class='el-input__inner' and @type='text']").send_keys("admin")
driver.find_element("xpath", "//input[@class='el-input__inner' and @type='password']").send_keys("yourpassword")
driver.find_element("xpath", "//button").click()
# 选择并确定
driver.find_element("xpath", "//span[@class='el-input__suffix']").click()
driver.find_element("xpath", "//div[@class='el-scrollbar']/div/ul/li[3]").click()
driver.find_element("xpath", "//button[@class='el-button el-button--primary el-button--small']").click()

# 登陆后进入菜单
revealed = driver.find_element("xpath", "//div[@class='el-scrollbar']")
wait = WebDriverWait(driver, timeout=4)
wait.until(lambda d : revealed.is_displayed())
# locate_with 无法定位
menu1 = locate_with("xpath", "//div[@class='menu-tree']").below({"xpath": "//li[@role='menuitem']//span[text()='系统管理']"})

driver.find_element("xpath", "//div[@class='menu-tree']//li[@role='menuitem']//span[text()='系统管理']").click()
driver.find_element("xpath", "//div[@class='menu-tree']//li[@role='menuitem' and @aria-expanded='true']/ul/label/div//span[text()='用户管理']").click()

revealed = driver.find_element("xpath", "//div[@class='cm-table']//div[@class='el-loading-mask']")
wait.until(lambda d : not revealed.is_displayed())
item = driver.find_element("xpath", "//table[@class='el-table__body']/tbody/tr/td//span[text()='王小梦']")
driver.execute_script("arguments[0].click();", item)

driver.find_element("xpath", "//div[@class='cm-button-group']/button//span[contains(text(), '查看详情')]").click()

sleep(1)
print('load finish')

执行效果:

《浏览器自动化测试工具Selenium的安装与试用》有2条评论

  1. My spouse and I stumbled over here from a different web
    address and thought I might check things out. I like what I see so now
    i am following you. Look forward to looking at your web page yet again.

    回复
  2. I was very pleased to discover this web site. I need to to thank you for your time due to this fantastic read!!
    I definitely loved every bit of it and I have you bookmarked to check out new stuff in your site.

    回复

发表评论

error: Content is protected !!