51Testing软件测试论坛

标题: python定位 [打印本页]

作者: 15889369825    时间: 2016-11-8 16:17
标题: python定位
<td>xpath=(//a[contains(text(),'编辑')])[15]</td>  selenium录制到的脚本,自己换了好多种定位,都没有定位到“编辑”,求解,新手刚学

<a href="#" class="green-class">编辑</a>这个是网页中看到的

作者: 若尘_51    时间: 2016-11-8 16:23
童靴,请将问题描述清楚些,起码包含以下的信息:             -
(1)页面相应部分截图 (2)相关脚本 (3)脚本报错信息

作者: 15889369825    时间: 2016-11-8 16:34
若尘_51 发表于 2016-11-8 16:23
童靴,请将问题描述清楚些,起码包含以下的信息:             -
(1)页面相应部分截图 (2)相关脚本 ( ...

# -*- 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.Firefox()
        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 + "/adminsystem/platform_user")
        driver.find_element_by_id("email").send_keys("2@163.com")
        driver.find_element_by_id("password").send_keys("123456")
        driver.find_element_by_id("submit").click()
        driver.find_element_by_link_text(u"系统管理").click()
        driver.find_element_by_link_text(u"平台子用户设置").click()
        driver.find_element_by_link_text(u"新增").click()
        driver.find_element_by_css_selector("div.form-group.button-group > button.btn.btn-blue").click()
        driver.find_element_by_name("email").clear()
        driver.find_element_by_name("email").send_keys("2@163.com")
        driver.find_element_by_css_selector("div.form-group.button-group > button.btn.btn-blue").click()
        driver.find_element_by_name("email").click()
        driver.find_element_by_name("email").clear()
        driver.find_element_by_name("email").send_keys("@163.com")
        driver.find_element_by_css_selector("div.form-group.button-group > button.btn.btn-blue").click()
        driver.find_element_by_name("email").click()
        driver.find_element_by_name("email").clear()
        driver.find_element_by_name("email").send_keys("79@163.com")
        driver.find_element_by_id("systemUserFormId").clear()
        driver.find_element_by_id("systemUserFormId").send_keys("123")
        driver.find_element_by_id("systemUserFormId").clear()
        driver.find_element_by_id("systemUserFormId").send_keys("!@#$123456")
        driver.find_element_by_id("systemUserFormId").clear()
        driver.find_element_by_id("systemUserFormId").send_keys("123456")
        driver.find_element_by_css_selector("div.modal-dialog.modal-type-1 > div.modal-content > div.modal-body > form.form-horizontal > div.form-group > input[name=\"repeat\"]").clear()
        driver.find_element_by_css_selector("div.modal-dialog.modal-type-1 > div.modal-content > div.modal-body > form.form-horizontal > div.form-group > input[name=\"repeat\"]").send_keys("123")
        driver.find_element_by_css_selector("div.modal-dialog.modal-type-1 > div.modal-content > div.modal-body > form.form-horizontal > div.form-group > input[name=\"repeat\"]").clear()
        driver.find_element_by_css_selector("div.modal-dialog.modal-type-1 > div.modal-content > div.modal-body > form.form-horizontal > div.form-group > input[name=\"repeat\"]").send_keys("123456")
        driver.find_element_by_name("employeeid").clear()
        driver.find_element_by_name("employeeid").send_keys("10086")
        driver.find_element_by_name("name").clear()
        driver.find_element_by_name("name").send_keys(u"张珊")
        driver.find_element_by_css_selector("span.label").click()
        driver.find_element_by_css_selector("li.last").click()
        driver.find_element_by_name("phone").clear()
        driver.find_element_by_name("phone").send_keys("1008611")
        driver.find_element_by_css_selector("div.form-group.button-group > button.btn.btn-blue").click()
        driver.find_element_by_css_selector(u"编辑").click()
        driver.find_element_by_name("employeeid").clear()
        driver.find_element_by_name("employeeid").send_keys("86")
        driver.find_element_by_name("name").clear()
        driver.find_element_by_name("name").send_keys(u"张珊珊")
        driver.find_element_by_css_selector("span.label").click()
        driver.find_element_by_css_selector("span.label").click()
        driver.find_element_by_name("phone").clear()
        driver.find_element_by_name("phone").send_keys("1008612")
        driver.find_element_by_css_selector("div.form-group.button-group > button.btn.btn-blue").click()
        driver.find_element_by_link_text(u"删除").click()
        driver.find_element_by_xpath("(//button[@type='button'])[10]").click()
        driver.find_element_by_id("user-btn-search").click()
        driver.find_element_by_id("user-search-input").clear()
        driver.find_element_by_id("user-search-input").send_keys(u"李")
   
    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()
作者: 若尘_51    时间: 2016-11-8 16:57
driver.find_element_by_css_selector(u"编辑").click()??? 这句有问题吧
作者: 15889369825    时间: 2016-11-8 17:01
若尘_51 发表于 2016-11-8 16:57
driver.find_element_by_css_selector(u"编辑").click()??? 这句有问题吧

恩,是有问题,这个是我自己写的,刚开始是这样定位的:driver.find_element_by_xpath(u"(//a[contains(text(),'编辑')])[10]").click()
作者: 15889369825    时间: 2016-11-8 17:09
若尘_51 发表于 2016-11-8 16:57
driver.find_element_by_css_selector(u"编辑").click()??? 这句有问题吧

是有问题,原先是这个driver.find_element_by_xpath(u"(//a[contains(text(),'编辑')])[10]").click(),也会报错,后面换了几种定位,都报错,不知道怎么错了
作者: 若尘_51    时间: 2016-11-8 17:22
15889369825 发表于 2016-11-8 17:09
是有问题,原先是这个driver.find_element_by_xpath(u"(//a[contains(text(),'编辑')])[10]").click(), ...

报错信息附上,还有那html的代码再给详细点~~
作者: 15889369825    时间: 2016-11-8 17:32
若尘_51 发表于 2016-11-8 17:22
报错信息附上,还有那html的代码再给详细点~~

Traceback (most recent call last):
  File "C:\Users\admin\Desktop\系统管理.py", line 57, in test_
    driver.find_element_by_class_name(u"(//a[contains(text(),'缂栬緫')])[15]").click()
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 413, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
    'value': value})['value']
  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)
InvalidSelectorException: Message: The given selector (//a[contains(text(),'\u7f16\u8f91')])[15] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: An invalid or illegal class name was specified
Stacktrace:
    at FirefoxDriver.annotateInvalidSelectorError_ (file:///c:/users/admin/appdata/local/temp/tmpyqeog5/extensions/fxdriver@googlecode.com/components/driver-component.js:10744)
    at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/admin/appdata/local/temp/tmpyqeog5/extensions/fxdriver@googlecode.com/components/driver-component.js:10775)
    at FirefoxDriver.prototype.findElement (file:///c:/users/admin/appdata/local/temp/tmpyqeog5/extensions/fxdriver@googlecode.com/components/driver-component.js:10779)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/admin/appdata/local/temp/tmpyqeog5/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/admin/appdata/local/temp/tmpyqeog5/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/< (file:///c:/users/admin/appdata/local/temp/tmpyqeog5/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)

----------------------------------------------------------------------
Ran 1 test in 27.662s

FAILED (errors=1)


html代码:
作者: 15889369825    时间: 2016-11-8 17:35
15889369825 发表于 2016-11-8 17:32
Traceback (most recent call last):
  File "C:%users\admin\Desktop\系统管理.py", line 57, in test_ ...

代码

作者: 掉渣饼    时间: 2016-11-9 09:07
你把定位编辑的那行代码换成下面这个试试看
driver.find_element_by_class_name("green-class").click()

作者: 若尘_51    时间: 2016-11-9 09:51
15889369825 发表于 2016-11-8 17:32
Traceback (most recent call last):
  File "C:%users\admin\Desktop\系统管理.py", line 57, in test_ ...

InvalidSelectorError: An invalid or illegal class name was specified.

driver.find_element_by_xpath(u"(//a[contains(text(),'编辑')])[10]").click()

是表达式错了,红色部分总感觉很别扭,具体也不知道哪里错了。

试试修改成:
driver.find_element_by_xpath(//a[@class='green-class']).click()   (不过前提是这个含class='green-class'的a标签只有一个)
作者: 15889369825    时间: 2016-11-9 10:04
若尘_51 发表于 2016-11-9 09:51
InvalidSelectorError: An invalid or illegal class name was specified.

driver.find_element_by_xp ...

好的,谢谢,我试试看
作者: 清晨一缕阳光    时间: 2016-11-9 10:34
可以使用classname定位,不过我估计这个页面中叫做"green-class"的会很多,不过可以加个[index],要地位的是第n个,就填写n-1就可以,数组的下标是从零开的。
作者: 15889369825    时间: 2016-11-9 18:23
清晨一缕阳光 发表于 2016-11-9 10:34
可以使用classname定位,不过我估计这个页面中叫做"green-class"的会很多,不过可以加个,要地位的是第n个 ...

厉害大神们,谢谢指教
作者: 15889369825    时间: 2016-11-15 10:06
掉渣饼 发表于 2016-11-9 09:07
你把定位编辑的那行代码换成下面这个试试看
driver.find_element_by_class_name("green-class").click()

还是不行




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2