51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1043|回复: 3
打印 上一主题 下一主题

selenium定位

[复制链接]
  • TA的每日心情
    开心
    2017-2-23 09:15
  • 签到天数: 13 天

    连续签到: 1 天

    [LV.3]测试连长

    跳转到指定楼层
    1#
    发表于 2016-12-24 16:56:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    # -*- 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邮箱的用户名和密码  定位不了   需要怎么定位

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    2021-12-24 13:41
  • 签到天数: 314 天

    连续签到: 1 天

    [LV.8]测试军长

    2#
    发表于 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)

    评分

    参与人数 1测试积点 +10 收起 理由
    lsekfe + 10 积极回复获得测试积点10

    查看全部评分

    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2017-2-23 09:15
  • 签到天数: 13 天

    连续签到: 1 天

    [LV.3]测试连长

    4#
     楼主| 发表于 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 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-10 20:17 , Processed in 0.073558 second(s), 25 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表