一个简单的登录网页上qq的脚本,通过此脚本了解到有些位置是无法通过xpath来定位的反倒是By定位更方便
#encoding=utf-8
from selenium import webdriver
import unittest,time
class QQmail(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox()
self.driver.get("https://mail.qq.com/")
def test_login(self):
driver=self.driver
driver.switch_to.frame("login_frame")#弹出一个frame来输入信息
driver.find_element_by_id("u").click()
driver.find_element_by_id("u").clear()#清楚默认值
driver.find_element_by_id("u").send_keys('qq号')
#driver.find_element_by_xpath("//*[@id="p"]").click()#用xpath无法识别p这个id
driver.find_element_by_id("p").click()
driver.find_element_by_id("p").clear()
driver.find_element_by_id("p").send_keys('qq密码')
driver.find_element_by_id("login_button").click()
def tearDown(self):
self.driver.quit()
if __name__=='__main__':
unittest.main()