15889369825 发表于 2016-11-24 10:56:37

求解

# -*- 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 Qi(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_qi(self):
      driver = self.driver
      driver.get(self.base_url + "/admindocmanage/admin_doc_center")
      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_id("search-input").click()
      driver.find_element_by_id("search-input").clear()
      driver.find_element_by_id("search-input").send_keys(u"想")
      driver.find_element_by_id("btn-search").click()
      driver.find_element_by_id("search-input").clear()
      driver.find_element_by_id("search-input").send_keys(u"狗")
      driver.find_element_by_id("btn-search").click()
      driver.find_element_by_id("search-input").clear()
      driver.find_element_by_id("search-input").send_keys("l")
      driver.find_element_by_id("btn-search").click()
      driver.find_element_by_id("search-input").clear()
      driver.find_element_by_id("search-input").send_keys(u"的")
      driver.find_element_by_id("btn-search").click()
      driver.find_element_by_id("addUser").click()
      driver.find_element_by_css_selector("span.label").click()
      driver.find_element_by_css_selector("li.last").click()
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/span").click()
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/div/ul/li").click()
      driver.find_element_by_id("chooseImg").click()
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/table/tbody/tr/td/img").click()
      driver.find_element_by_class_name("up_text").send_keys(u"F:/cylan-2.4.9.840-2016111818-(test.jfgou.com)-Inhouse)")
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/span").click()
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/div/ul/ul/li").click()
      driver.find_element_by_name("abstract").clear()
      driver.find_element_by_name("abstract").send_keys(u"221谔谔")
      driver.find_element_by_id("journal").clear()
      driver.find_element_by_id("journal").send_keys(u"2呃3呃3呃呃")
#      driver.find_element_by_css_selector("#sizzle1479805746127 > label.radio-label.form-label1").click()
      driver.find_element_by_id("sendEmail").click()
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/span").click()
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/div/ul/li").click()
      driver.find_element_by_id("confirm").click()
#      driver.find_element_by_id("search-input").click()
#      driver.find_element_by_id("search-input").clear()
      driver.find_element_by_id("search-input").send_keys("")
#      driver.find_element_by_id("btn-search").click()
      driver.find_element_by_class_name(u"编辑").click()
      driver.find_element_by_name("abstract").clear()
      driver.find_element_by_name("abstract").send_keys(u"221谔谔;/i/;")
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/span").click()
      driver.find_element_by_css_selector("ul.selectric-group > li.last").click()
      driver.find_element_by_id("journal").clear()
      driver.find_element_by_id("journal").send_keys(u"2呃3呃3呃呃i/i/i/i//i/;/;/")
      driver.find_element_by_id("confirm").click()
      driver.find_element_by_link_text(u"操作日志").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()
报这个错误
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\admin\Desktop\qi.py", line 62, in test_qi
    driver.find_element_by_xpath(u"(//a)").click()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
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)
NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"(//a)"}
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/admin/appdata/local/temp/tmp8jg6hk/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///c:/users/admin/appdata/local/temp/tmp8jg6hk/extensions/fxdriver@googlecode.com/components/driver-component.js:625)

----------------------------------------------------------------------
Ran 1 test in 52.406s

FAILED (errors=1)
>>>
=================== RESTART: C:\Users\admin\Desktop\qi.py ===================
E
======================================================================
ERROR: test_qi (__main__.Qi)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\admin\Desktop\qi.py", line 62, in test_qi
    driver.find_element_by_class_name(u"缂栬緫").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)
NoSuchElementException: Message: Unable to locate element: {"method":"class name","selector":"\u7f16\u8f91"}
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/admin/appdata/local/temp/tmpi6zugx/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///c:/users/admin/appdata/local/temp/tmpi6zugx/extensions/fxdriver@googlecode.com/components/driver-component.js:625)

----------------------------------------------------------------------
Ran 1 test in 50.527s

FAILED (errors=1)


这个运行现象是没有上传到文件    但报的错误是编辑那个地方,但编辑那个地方换了class_namelink_text 几种定位都报错求解




若尘_51 发表于 2016-11-24 11:22:51

driver.find_element_by_class_name(u"编辑").click() ???
应该是 driver.find_element_by_link_text(u"编辑").click(),不过你这里太多编辑的链接了,用不了这种方法
你试试xpath的定位方式吧

15889369825 发表于 2016-11-24 11:37:10

若尘_51 发表于 2016-11-24 11:22
driver.find_element_by_class_name(u"编辑").click() ???
应该是 driver.find_element_by_link_text ...

换成driver.find_element_by_xpath("//a[@class='green-class']").click()这个单独跑编辑这块没有问题,但下面 这块跑,就报 driver.find_element_by_xpath("//a[@class='green-class']").click() 错误
driver.find_element_by_id("addUser").click()
      driver.find_element_by_css_selector("span.label").click()
      driver.find_element_by_css_selector("li.last").click()
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/span").click()
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/div/ul/li").click()
      driver.find_element_by_id("chooseImg").click()
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/table/tbody/tr/td/img").click()
      driver.find_element_by_class_name("up_text").send_keys(u"F:/cylan-2.4.9.840-2016111818-(test.jfgou.com)-Inhouse)")
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/span").click()
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/div/ul/ul/li").click()
      driver.find_element_by_name("abstract").clear()
      driver.find_element_by_name("abstract").send_keys(u"221谔谔")
      driver.find_element_by_id("journal").clear()
      driver.find_element_by_id("journal").send_keys(u"2呃3呃3呃呃")
#      driver.find_element_by_css_selector("#sizzle1479805746127 > label.radio-label.form-label1").click()
      driver.find_element_by_id("sendEmail").click()
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/span").click()
      driver.find_element_by_xpath("//div[@id='modal-body-dialog']/form/div/div/div/div/ul/li").click()
      driver.find_element_by_id("confirm").click()
#      driver.find_element_by_id("search-input").click()
#      driver.find_element_by_id("search-input").clear()
      driver.find_element_by_id("search-input").send_keys("")
#      driver.find_element_by_id("btn-search").click()

若尘_51 发表于 2016-11-24 13:46:14

15889369825 发表于 2016-11-24 11:37
换成driver.find_element_by_xpath("//a[@class='green-class']").click()这个单独跑编辑这块没有问题 ...

driver.find_element_by_xpath("//a[@class='green-class']").click()
使用这个应该定位到序号为1的编辑吧?

你是怎么单独跑可以成功的?

15889369825 发表于 2016-11-24 13:48:38

若尘_51 发表于 2016-11-24 13:46
driver.find_element_by_xpath("//a[@class='green-class']").click()
使用这个应该定位到序号为1的编辑 ...

单独跑注销那段代码,直接跑编辑开始,你说的序号加1怎么加

若尘_51 发表于 2016-11-24 14:15:47

15889369825 发表于 2016-11-24 13:48
单独跑注销那段代码,直接跑编辑开始,你说的序号加1怎么加

试试:driver.find_element_by_xpath("//a[@class='green-class']").click()

15889369825 发表于 2016-11-24 14:31:58

若尘_51 发表于 2016-11-24 14:15
试试:driver.find_element_by_xpath("//a[@class='green-class']").click()

不行,还是报错
Traceback (most recent call last):
File "C:\Users\admin\Desktop\qi.py", line 62, in test_qi
    driver.find_element_by_xpath("//a[@class='green-class']").click()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
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)
NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//a[@class='green-class']"}
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/admin/appdata/local/temp/tmpus9fcw/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///c:/users/admin/appdata/local/temp/tmpus9fcw/extensions/fxdriver@googlecode.com/components/driver-component.js:625)

若尘_51 发表于 2016-11-24 14:46:00

15889369825 发表于 2016-11-24 14:31
不行,还是报错
Traceback (most recent call last):
File "C:%users\admin\Desktop\qi.py", line 62 ...

你把那个编辑选项,详细的HTML路径给我发一下(如:/html/body/.......)

15889369825 发表于 2016-11-24 15:04:41

若尘_51 发表于 2016-11-24 14:46
你把那个编辑选项,详细的HTML路径给我发一下(如:/html/body/.......)

22

若尘_51 发表于 2016-11-24 15:16:07

15889369825 发表于 2016-11-24 15:04
22

再试试这个:      driver.find_element_by_xpath("//div[@data-id='52']/td/a").click()

15889369825 发表于 2016-11-24 15:20:38

若尘_51 发表于 2016-11-24 15:16
再试试这个:      driver.find_element_by_xpath("//div[@data-id='52']/td/a").click()

我觉得不是这个编辑问题,而是这个driver.find_element_by_class_name("up_text").send_keys(u"F:/cylan-2.4.9.840-2016111818-(test.jfgou.com)-Inhouse)")文件没有上传上去导致,因为我的页面这里没有成功上传文件点击确认就会报错

若尘_51 发表于 2016-11-24 15:24:43

15889369825 发表于 2016-11-24 15:20
我觉得不是这个编辑问题,而是这个driver.find_element_by_class_name("up_text").send_keys(u"F:/cylan- ...

报错信息是到编辑这里出错的,你跑一下脚本,看看到底是到哪一步失败了。
这个一步一步调吧, 其他还没有涉及的地方先屏蔽~~~

15889369825 发表于 2016-11-24 15:36:38

若尘_51 发表于 2016-11-24 15:24
报错信息是到编辑这里出错的,你跑一下脚本,看看到底是到哪一步失败了。
这个一步一步调吧, 其他还 ...

跑的脚本是页面显示的是上传文件那里报错,导致进行不下去,但看报错的信息是编辑那里错误,只能一步步调试了,真是谢谢了帮忙想了这么多办法
页: [1]
查看完整版本: 求解