15889369825 发表于 2016-12-24 16:56:39

selenium定位

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class test (unittest.TestCase):
    def setUp(self):
      self.driver = webdriver.Chrome()
      self.driver.implicitly_wait(30)
      self.base_url = "http://test.robotscloud.com/"
      self.verificationErrors = []
      self.accept_next_alert = True
   
    def test_(self):
      driver = self.driver
      driver.get(self.base_url + "/#")
      driver.find_element_by_link_text(u"忘记密码").click()
      driver.find_element_by_id("submit-find-back").click()
      driver.find_element_by_id("email").clear()
      driver.find_element_by_id("email").send_keys("123456789@163")
      driver.find_element_by_id("submit-find-back").click()
      driver.find_element_by_id("email").clear()
      driver.find_element_by_id("email").send_keys("123456789@163.com")
      driver.find_element_by_id("submit-find-back").click()
      driver.find_element_by_id("authcode").clear()
      driver.find_element_by_id("authcode").send_keys("3333")
      driver.find_element_by_id("submit-find-back").click()
      driver.find_element_by_css_selector("img.captcha-img").click()
      driver.find_element_by_id("email").click()
      driver.find_element_by_id("email").clear()
      driver.find_element_by_id("email").send_keys("")
      driver.find_element_by_id("submit-find-back").click()
      driver.find_element_by_id("email").click()
      driver.find_element_by_id("email").clear()
      driver.find_element_by_id("email").send_keys("cylan_123@@163.com")
      driver.find_element_by_id("submit-find-back").click()
      driver.find_element_by_id("email").clear()
      driver.find_element_by_id("email").send_keys("cylan_123@163.com")
      driver.find_element_by_id("authcode").clear()
      driver.find_element_by_id("authcode").send_keys("333")
      driver.find_element_by_id("submit-find-back").click()
      driver.find_element_by_id("authcode").clear()
      driver.find_element_by_id("authcode").send_keys("")
      driver.find_element_by_id("submit-find-back").click()
      driver.find_element_by_id("authcode").clear()
      driver.find_element_by_id("authcode").send_keys("3333")
      driver.find_element_by_id("submit-find-back").click()
      driver.find_element_by_id("to-login-email").click()
      driver.find_element_by_id("email").clear()
      driver.find_element_by_id("email").send_keys("cylan_123@163.com")
      driver.find_element_by_id("authcode").clear()
      driver.find_element_by_id("authcode").send_keys("7616")
      driver.find_element_by_id("submit-find-back").click()
      driver.find_element_by_id("login-email").click()
      driver.find_element_by_id("username").click
      time.sleep(2)
      driver.find_element_by_id("username").send_keys("cylan_123@163.com")
      time.sleep(2)
      driver.find_element_by_id("password").click
      time.sleep(2)
      driver.find_element_by_id("password").send_keys("cylan123456")
      time.sleep(2)
      driver.find_element_by_id()
      driver.find_element_by_id("dologin").click()
      driver.find_element_by_link_text(u"立即重置密码").click()
      driver.find_element_by_id("reset-submit").click()
      driver.find_element_by_id("reset-submit").click()
      driver.find_element_by_id("password").clear()
      driver.find_element_by_id("password").send_keys("123")
      driver.find_element_by_id("reset-submit").click()
      driver.find_element_by_id("password").clear()
      driver.find_element_by_id("password").send_keys("~!@qwe123")
      driver.find_element_by_id("reset-submit").click()
      driver.find_element_by_id("password").clear()
      driver.find_element_by_id("password").send_keys("123456")
      driver.find_element_by_id("repeat").clear()
      driver.find_element_by_id("repeat").send_keys("`12")
      driver.find_element_by_id("reset-submit").click()
      driver.find_element_by_id("repeat").clear()
      driver.find_element_by_id("repeat").send_keys("~!@#$123456")
      driver.find_element_by_id("reset-submit").click()
      driver.find_element_by_id("repeat").clear()
      driver.find_element_by_id("repeat").send_keys("")
      driver.find_element_by_id("reset-submit").click()
      driver.find_element_by_id("repeat").clear()
      driver.find_element_by_id("repeat").send_keys("123456")
      driver.find_element_by_id("reset-submit").click()
      driver.find_element_by_css_selector("span.close").click()
   
    def is_element_present(self, how, what):
      try: self.driver.find_element(by=how, value=what)
      except NoSuchElementException as e: return False
      return True
   
    def is_alert_present(self):
      try: self.driver.switch_to_alert()
      except NoAlertPresentException as e: return False
      return True
   
    def close_alert_and_get_its_text(self):
      try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
      finally: self.accept_next_alert = True
   
    def tearDown(self):
      self.driver.quit()
      self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
    unittest.main()


我这样定位163邮箱的用户名和密码定位不了   需要怎么定位

掉渣饼 发表于 2016-12-24 20:48:01

因为下面这段在页面源码中没有username这个id,以及click方法你写的也不对啊,在163登录首页用户名输入框父节点上面还有iframe,可以做如下替换
driver.find_element_by_id("username").click
time.sleep(2)
driver.find_element_by_id("username").send_keys("cylan_123@163.com")
time.sleep(2)
driver.find_element_by_id("password").click
time.sleep(2)
driver.find_element_by_id("password").send_keys("cylan123456")
time.sleep(2)
driver.find_element_by_id()
替换成
driver.switch_to_frame("x-URS-iframe")
time.sleep(3)
driver.find_element_by_name("email").clear()
driver.find_element_by_name("email").send_keys("cylan_123")
driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys("cylan123456")
time.sleep(3)

黑盒测试 发表于 2016-12-25 11:28:48

学习

15889369825 发表于 2016-12-26 09:48:38

掉渣饼 发表于 2016-12-24 20:48
因为下面这段在页面源码中没有username这个id,以及click方法你写的也不对啊,在163登录首页用户名输入框父 ...

Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\忘记密码.py", line 59, in test_
    driver.switch_to_frame("x-URS-iframe")
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 574, in switch_to_frame
    self._switch_to.frame(frame_reference)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 84, in frame
    self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
NoSuchFrameException: Message: no such frame
(Session info: chrome=54.0.2840.99)
(Driver info: chromedriver=2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf),platform=Windows NT 6.1.7601 SP1 x86)


----------------------------------------------------------------------
Ran 1 test in 9.264s

FAILED (errors=1)   
也不行报错了
页: [1]
查看完整版本: selenium定位