from selenium import webdriver
import time
import random
#导入expected_conditions预期包判断标题是否正确:
from selenium.webdriver.support import expected_conditions as EC
#导入WebDriverWait
from selenium.webdriver.support.wait import WebDriverWait
#导入by:
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.maximize_window()
# driver.get("http://www.5itest.cn/register")
# EC.title_is #title_is代表现在的title和预期的title是一样的
time.sleep(5)
# print(EC.title_contains("注册")) #title_contains代表只要包含就可以了
#拿到邮箱地址:
# email_element = driver.find_element_by_id("register_email")
#随机生成字符串5位数并拼接:
for i in range(5):
user_email = ''.join(random.sample("1234567890abcdefg",5))+"@163.com"
print(user_email)
element = driver.find_element_by_class_name("controls")
#拿到By定位的元素:
locator = (By.CLASS_NAME,"controls")
#visibility_of_element_located使用可见度/是否可见:
# EC.visibility_of_element_located(element)
#使用WebDriverWait的until寻找父级元素:
WebDriverWait(driver,1).until(EC.visibility_of_element_located(locator))
#取文字属性信息:
print(email_element.get_attribute("placeholder"))
#输入信息:
email_element.send_keys("test@163.com")
#获取值:
print(email_element.get_attribute("value"))
#关闭driver可以有效避免电脑和浏览器卡顿:
driver.close()
#presence_of_element_located元素的存在位置:
EC.presence_of_element_located()
#使用id进行定位邮箱地址:
# driver.find_element_by_id("register_email").send_keys("1320685524@qq.com")
#使用class定位用户名、find_elements_by_class_name返回的是列表、列表才可以len、先定义父类、再定义子类:
# user_name_element_node = driver.find_elements_by_class_name("controls")[1]
# user_element = user_name_element_node.find_element_by_class_name("form-control")
# user_element.send_keys("dsads")
#使用name定位密码:
# driver.find_element_by_name("password").send_keys("990496256dd")
#使用xpath定位验证码:
# driver.find_element_by_xpath('//*[@id="captcha_code"]').send_keys("111111")