selenium2自动化测试学习笔记(三)

时间:2025-01-03 00:04:50

今天是学习selenium的第三天,今天的主题是自动登录126邮箱。

今天总结碰到的坑有三个:

1、frame内元素抓取,使用driver.switch_to.frame(frameId)方法切换锁定frame

2、等待页面渲染完毕定位输入,chrome下使用sleep(秒)等待

3、 driver的element内置方法会有失灵情况,采用driver.execute_script(js字符串)方法执行

初始代码,代码中的username\password替换成自己的账户密码即可

 from selenium import webdriver

 #自动打开邮箱主页登录
driver = webdriver.Firefox()
#driver = webdriver.Chrome()
#driver = webdriver.Ie()
dr.get("http://mail.126.com")
idInput = dr.find_element_by_xpath('//input[@name="email"]')
pwdInput = dr.find_element_by_xpath('//input[@name="password"]')
idInput.clear()
idInput.send_keys("username")
pwdInput.clear()
pwdInput.send_keys("password") dr.find_element_by_id('dologin').click() dr.quit()

搞了半天,怎么都获取不到,然后抓取本页面的所有input,循环打印出来,发现确实找不到登录框。

inputs = driver.find_elements_by_xpath("//input")
print( len(inputs) )
for input in inputs:
print('input id is ',input.get_attribute("id"),
'| input name is ',input.get_attribute("name") )

眼瞎的后面才发现是嵌套在iframe中的,这就好办了,网上搜索了下定位方法

#driver.switch_to.frame("x-URS-iframe")#定位到登录frame
print('begin定位到x-URS-iframe')
driver.switch_to.frame("x-URS-iframe")
print('end定位到x-URS-iframe')

接下来的代码都一样,但是在email框中send_keys始终无法输入成功,password却可以,莫名其妙,没搞懂。

此时换成执行js方法

dologin = driver.find_element_by_id("dologin")
email = driver.find_element_by_name("email")
password = driver.find_element_by_name("password") print('email is_displayed ',email.is_displayed() )
print('email is_enabled ',email.is_enabled() )
email.clear()
#email.send_keys('kiokyw')#火狐此方法不行 #调用js 输入
email_id = email.get_attribute("id")
js = "document.getElementById('"+email_id+"').value='username'"
print(js)
driver.execute_script(js)

  执行成功,再输入password,并点击登录按钮

 password.clear()
password.send_keys('password') print( 'email class = ', email.get_attribute('class') )
print( 'email value = ', email.get_attribute('value') )
print( 'password value = ', password.get_attribute('value') ) js = "document.getElementById('dologin').click()"
print(js)
driver.execute_script(js)
#dologin.click()#IE下此方法不行
print('登录成功')

  最后是切回主frame,否则还在原来的frame中,获取不到新页面的元素

#切回主frame
driver.switch_to.default_content() #当前url
now_url = driver.current_url
print(now_url) #获取当前登录用户名
user = driver.find_element_by_id('spnUid').text
print(user)

  最后是完整代码

 from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import ctime
from time import sleep driver = webdriver.Firefox()
#driver = webdriver.Chrome()
#driver = webdriver.Ie() #设置隐式等待为10秒
driver.implicitly_wait(10) url = 'http://www.126.com'
driver.get(url)
print("title is ",driver.title) #element = WebDriverWait( driver,5, 0.5).until(
# EC.presence_of_element_located((By.NAME,'wd'))
# ) '''
try:
idInput = driver.find_elements_by_xpath('//a')
#等同
#idInput = driver.find_elements_by_css_selector('a')
print(len(idInput))
for a in idInput:
print(a.get_attribute('id') )
except:
print("test error") try: element = WebDriverWait( driver,10, 0.5).until(
EC.presence_of_element_located((By.ID,'dologin'))
)
idInput = driver.find_elements_by_css_selector('input')
print(len(idInput))
for a in idInput:
print(a.get_attribute('id') )
except:
print("test error")
'''
#定位到iframe
try:
sleep(10) #chrome下需要等待
print( ctime(),'| url=',driver.current_url )
#driver.switch_to.frame("x-URS-iframe")#定位到登录frame
print('begin定位到x-URS-iframe')
driver.switch_to.frame("x-URS-iframe")
print('end定位到x-URS-iframe') inputs = driver.find_elements_by_xpath("//input")
print( len(inputs) )
for input in inputs:
print('input id is ',input.get_attribute("id"),
'| input name is ',input.get_attribute("name") ) dologin = driver.find_element_by_id("dologin")
email = driver.find_element_by_name("email")
password = driver.find_element_by_name("password") print('email is_displayed ',email.is_displayed() )
print('email is_enabled ',email.is_enabled() )
email.clear()
#email.send_keys('kiokyw')#火狐此方法不行 #调用js 输入成功 email_id = email.get_attribute("id")
js = "document.getElementById('"+email_id+"').value='kiokyw'"
print(js)
driver.execute_script(js) password.clear()
password.send_keys('Yy19861121') print( 'email class = ', email.get_attribute('class') )
print( 'email value = ', email.get_attribute('value') )
print( 'password value = ', password.get_attribute('value') ) js = "document.getElementById('dologin').click()"
print(js)
driver.execute_script(js)
#dologin.click()#IE下此方法不行
print('登录成功') #切回主frame
driver.switch_to.default_content() #当前url
now_url = driver.current_url
print(now_url) #获取当前登录用户名
user = driver.find_element_by_id('spnUid').text
print(user) except BaseException as msg:
print(msg) print("关闭浏览器")
driver.quit()

以上代码太长了,再另附一份谷歌登录163邮箱的代码,重点是需要等待渲染

 from selenium import webdriver
from time import sleep #自动打开邮箱主页登录
#dr= webdriver.Firefox()
dr= webdriver.Chrome()
#dr = webdriver.Ie()
dr.get("http://mail.163.com")
sleep(2)
dr.switch_to.frame("x-URS-iframe")
idInput = dr.find_element_by_xpath('//input[@name="email"]')
pwdInput = dr.find_element_by_xpath('//input[@name="password"]')
idInput.clear()
idInput.send_keys("username")
pwdInput.clear()
pwdInput.send_keys("password") dr.find_element_by_id('dologin').click() dr.quit()