兔展首页登录练习

实例网站:http://www.rabbitpre.com

源码显示:

1.通过by_xpath的方法

了解xpath表达式:该文章来自于ray蜀黍:http://www.cnblogs.com/tsbc/p/6530491.html

 1 #coding=utf-8
 2 from selenium import webdriver
 3 from selenium.webdriver.common.keys import Keys
 4 import  os,time
 5 driver=webdriver.Chrome()
 6 driver.get("http://www.rabbitpre.com")
 7 time.sleep(3)
 8 driver.maximize_window()
 9 #打开登陆弹窗
10 driver.find_element_by_css_selector("span[class="login j-login"]").click()
11 time.sleep(1)
12 #将当前识别主体移动到第一个的frame上去
13 driver.switch_to_frame(1)
14 #输入用户名
15 driver.find_element_by_xpath("//div[@id='LOGREG_1000']//input[@class='user-account']").clear()
16 driver.find_element_by_xpath("//input[@class='user-account']").send_keys("18503001535")
17 #输入密码
18 driver.find_element_by_xpath("//div[@id='LOGREG_1000']//div[contains(@class,'login-container')]//input[@class='user-pass']").clear()
19 driver.find_element_by_xpath("//div[contains(@class,'login-container')]//input[@class='user-pass']").send_keys("111111")
20 #登陆
21 driver.find_element_by_xpath("//div[@id='LOGREG_1000']//div[contains(@class,'login-container')]//button").click()
22 time.sleep(3)
23 driver.quit()

 详解:

  因为在该页面密码框在注册与登录都有相同的密码框,且属性相同,所以我们在写xpath表达式的时候需要区分下

#将当前识别主体移动到第一个的frame上去
el=driver.find_element_by_xpath('//div[@id="DIALOG_1000"]/div[1]/div[1]/iframe')
driver.switch_to.frame(el)
原文地址:https://www.cnblogs.com/8013-cmf/p/6532606.html