今天是学习selenium的第三天,今天的主题是自动登录126邮箱。
今天总结碰到的坑有三个:
1、frame内元素抓取,使用driver.switch_to.frame(frameId)方法切换锁定frame
2、等待页面渲染完毕定位输入,chrome下使用sleep(秒)等待
3、 driver的element内置方法会有失灵情况,采用driver.execute_script(js字符串)方法执行
初始代码,代码中的usernamepassword替换成自己的账户密码即可
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from selenium import webdriver 2 3 #自动打开邮箱主页登录 4 driver = webdriver.Firefox() 5 #driver = webdriver.Chrome() 6 #driver = webdriver.Ie() 7 dr.get("http://mail.126.com") 8 idInput = dr.find_element_by_xpath('//input[@name="email"]') 9 pwdInput = dr.find_element_by_xpath('//input[@name="password"]') 10 idInput.clear() 11 idInput.send_keys("username") 12 pwdInput.clear() 13 pwdInput.send_keys("password") 14 15 dr.find_element_by_id('dologin').click() 16 17 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)
最后是完整代码
1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 from selenium.webdriver.support.ui import WebDriverWait 4 from selenium.webdriver.support import expected_conditions as EC 5 from time import ctime 6 from time import sleep 7 8 driver = webdriver.Firefox() 9 #driver = webdriver.Chrome() 10 #driver = webdriver.Ie() 11 12 #设置隐式等待为10秒 13 driver.implicitly_wait(10) 14 15 url = 'http://www.126.com' 16 driver.get(url) 17 print("title is ",driver.title) 18 19 #element = WebDriverWait( driver,5, 0.5).until( 20 # EC.presence_of_element_located((By.NAME,'wd')) 21 # ) 22 23 ''' 24 try: 25 idInput = driver.find_elements_by_xpath('//a') 26 #等同 27 #idInput = driver.find_elements_by_css_selector('a') 28 print(len(idInput)) 29 for a in idInput: 30 print(a.get_attribute('id') ) 31 except: 32 print("test error") 33 34 try: 35 36 element = WebDriverWait( driver,10, 0.5).until( 37 EC.presence_of_element_located((By.ID,'dologin')) 38 ) 39 idInput = driver.find_elements_by_css_selector('input') 40 print(len(idInput)) 41 for a in idInput: 42 print(a.get_attribute('id') ) 43 except: 44 print("test error") 45 ''' 46 #定位到iframe 47 try: 48 sleep(10) #chrome下需要等待 49 print( ctime(),'| url=',driver.current_url ) 50 #driver.switch_to.frame("x-URS-iframe")#定位到登录frame 51 print('begin定位到x-URS-iframe') 52 driver.switch_to.frame("x-URS-iframe") 53 print('end定位到x-URS-iframe') 54 55 56 inputs = driver.find_elements_by_xpath("//input") 57 print( len(inputs) ) 58 for input in inputs: 59 print('input id is ',input.get_attribute("id"), 60 '| input name is ',input.get_attribute("name") ) 61 62 63 dologin = driver.find_element_by_id("dologin") 64 email = driver.find_element_by_name("email") 65 password = driver.find_element_by_name("password") 66 67 print('email is_displayed ',email.is_displayed() ) 68 print('email is_enabled ',email.is_enabled() ) 69 email.clear() 70 #email.send_keys('kiokyw')#火狐此方法不行 71 72 #调用js 输入成功 73 74 email_id = email.get_attribute("id") 75 js = "document.getElementById('"+email_id+"').value='kiokyw'" 76 print(js) 77 driver.execute_script(js) 78 79 80 password.clear() 81 password.send_keys('Yy19861121') 82 83 print( 'email class = ', email.get_attribute('class') ) 84 print( 'email value = ', email.get_attribute('value') ) 85 print( 'password value = ', password.get_attribute('value') ) 86 87 88 js = "document.getElementById('dologin').click()" 89 print(js) 90 driver.execute_script(js) 91 #dologin.click()#IE下此方法不行 92 print('登录成功') 93 94 95 #切回主frame 96 driver.switch_to.default_content() 97 98 #当前url 99 now_url = driver.current_url 100 print(now_url) 101 102 #获取当前登录用户名 103 user = driver.find_element_by_id('spnUid').text 104 print(user) 105 106 except BaseException as msg: 107 print(msg) 108 109 print("关闭浏览器") 110 driver.quit()
以上代码太长了,再另附一份谷歌登录163邮箱的代码,重点是需要等待渲染
1 from selenium import webdriver 2 from time import sleep 3 4 #自动打开邮箱主页登录 5 #dr= webdriver.Firefox() 6 dr= webdriver.Chrome() 7 #dr = webdriver.Ie() 8 dr.get("http://mail.163.com") 9 sleep(2) 10 dr.switch_to.frame("x-URS-iframe") 11 idInput = dr.find_element_by_xpath('//input[@name="email"]') 12 pwdInput = dr.find_element_by_xpath('//input[@name="password"]') 13 idInput.clear() 14 idInput.send_keys("username") 15 pwdInput.clear() 16 pwdInput.send_keys("password") 17 18 dr.find_element_by_id('dologin').click() 19 20 dr.quit()