Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python url是通过selenium IDE正确启动的,但不是通过脚本_Python_Python 2.7_Selenium_Selenium Webdriver_Geckodriver - Fatal编程技术网

Python url是通过selenium IDE正确启动的,但不是通过脚本

Python url是通过selenium IDE正确启动的,但不是通过脚本,python,python-2.7,selenium,selenium-webdriver,geckodriver,Python,Python 2.7,Selenium,Selenium Webdriver,Geckodriver,我正在尝试启动一个url,一旦加载将提示输入用户名和密码 问题是,当我通过SeleniumIDE记录和运行测试用例时,它是有效的。 但是,当我试图通过代码启动url时,它会变成灰色,并且不会显示任何输入用户名和密码的提示 观察: from selenium import webdriver fp = webdriver.FirefoxProfile('C:/Users/<user name>/AppData/Roaming/Mozilla/Firefox/Profiles/abc3d

我正在尝试启动一个url,一旦加载将提示输入用户名和密码

问题是,当我通过SeleniumIDE记录和运行测试用例时,它是有效的。 但是,当我试图通过代码启动url时,它会变成灰色,并且不会显示任何输入用户名和密码的提示

观察:

from selenium import webdriver
fp = webdriver.FirefoxProfile('C:/Users/<user name>/AppData/Roaming/Mozilla/Firefox/Profiles/abc3defghij2.ProfileName')
driver = webdriver.Firefox(fp)
driver.get("https://www.example.com/membersarea")
当我手动打开URL时

url =http://10.131.14.245:30016/index.html
它会在几秒钟内自动重定向到

http://10.131.14.245:30016/index.html#/Login
用户名和密码提示看起来是可见的

然而,在脚本执行期间,URL并没有重定向自身,而是保持不变

url =http://10.131.14.245:30016/index.html
即使我们通过代码添加值“#Login”,它也不起作用

我正在使用 Python 2.7 硒3.6 火狐52.4 geckodriver v.018

下面是geckodriver.log文件中的错误

1507793622480   geckodriver INFO    geckodriver 0.18.0
1507793622495   geckodriver INFO    Listening on 127.0.0.1:56485
1507793624523   geckodriver::marionette INFO    Starting browser C:\Program Files\Mozilla Firefox\firefox.exe with args ["-marionette"]
1507793626284   Marionette  INFO    Listening on port 56493
1507793626626   Marionette  WARN    TLS certificate errors will be ignored for this session
JavaScript warning: http://10.131.14.245:30016/vendor/jquery/jquery.min.js, line 1: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead
JavaScript error: http://10.131.14.245:30016/vendor/requirejs/require.js, line 165: Error: Load timeout for modules: angular,ocLazyLoad,angular-ui-router,angular-translate,angular-sanitize,angular-translate-loader-static-files,angular-translate-loader-partial,angular-css-injector
http://requirejs.org/docs/errors.html#timeout
#timeout
1507793619059   addons.productaddons    WARN    Failed downloading via XHR, status: 0, reason: error
1507793619078   addons.productaddons    WARN    Failed downloading via XHR, status: 0, reason: error
[Child 12096] WARNING: pipe error: 109: file c:/builds/moz2_slave/m-esr52-w64-000000000000000000/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 345
下面是python中的错误

D:\userdata\khchoudh\Desktop\POC\Selenium Robotframework\Firefox>python worked2.py
EE
======================================================================
ERROR: test_worked2 (__main__.Worked2)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "worked2.py", line 21, in test_worked2
    driver.find_element_by_name("username").clear()
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 437, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 843, in find_element
    'value': value})['value']
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Unable to locate element: [name="username"]
Python代码

--编码:utf-8-- 网站的HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title ng-bind="title">OpenDayLight DLUX</title>

    <meta name="description" content="overview &amp; stats" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script type="text/javascript">
var module = ['angular','ocLazyLoad','angular-ui-router','angular-translate', 'angular-sanitize', 'angular-translate-loader-static-files', 'angular-translate-loader-partial', 'angular-css-injector'];
var deps = ['common/config/env.module','app/testsetup/testsetup.module','app/core/core.module','app/designboard/designboard.module','app/testautomation/testautomation.module','common/login/login.module','common/authentification/auth.module','common/navigation/navigation.module','common/topbar/topbar.module','common/general/common.general.module','app/topology/topology.module','app/yangui/main','common/yangutils/yangutils.module','common/sigmatopology/sigmatopology.module','app/yangvisualizer/yangvisualizer.module','common/layout/layout.module'];
var e = ['oc.lazyLoad', 'ui.router', 'pascalprecht.translate', 'ngSanitize', 'angular.css.injector', 'app','app.testsetup','app.core','app.designboard','app.testautomation','app.common.login','app.common.auth','app.common.nav','app.common.topbar','app.common.general','app.topology','app.yangui','app.common.yangUtils','app.common.sigmatopology','app.yangvisualizer','app.common.layout'];
        // global variables

    </script>

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->

    <!--[if lt IE 9]>
    <script src="assets/js/html5shiv.js"></script>
    <script src="assets/js/respond.min.js"></script>
    <![endif]-->

    <!-- compiled CSS -->
    <link rel="stylesheet" type="text/css" href="vendor/ng-grid/ng-grid.min.css" />
    <link rel="stylesheet" type="text/css" href="vendor/select2-bootstrap-css/select2-bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="vendor/footable/css/footable.core.min.css" />
    <link rel="stylesheet" type="text/css" href="vendor/footable/css/footable.standalone.min.css" />
    <link rel="stylesheet" type="text/css" href="vendor/vis/dist/vis.min.css" />
    <link rel="stylesheet" type="text/css" href="vendor/ng-slider/dist/css/ng-slider.min.css" />
    <link rel="stylesheet" type="text/css" href="assets/opendaylight-dlux-0.2.0.css" />
    <link rel="stylesheet" href="assets/css/sb-admin.css" />

    <script type="text/javascript" data-main="src/main.js" src="vendor/requirejs/require.js"></script>

    <link rel="stylesheet" href="assets/css/font-awesome.min.css" />
    <!-- the font-awesome is different from the 'official' one -->
<link rel="stylesheet" href="src/app/testsetup/testsetup-custom.css" />
<link rel="stylesheet" href="src/app/designboard/designboard-custom.css" />
<link rel="stylesheet" href="src/app/testautomation/testautomation-custom.css" />

    <!-- application CSS -->
  </head>

  <body class="skin-3">
  <div ui-view="mainContent"></div>
  </body>
</html>

开放日DLUX
变量模块=['angular','ocLazyLoad','angular-ui-router','angular-translate','angular-sanitize','angular-translate加载程序静态文件','angular-translate加载程序部分','angular-css注入器'];
var deps=['common/config/env.module'、'app/testsetup/testsetup.module'、'app/core/core.module'、'app/designboard/designboard.module'、'app/testautomation/testautomation.module'、'common/login/login.module'、'common/authentication/auth.module'、'common/navigation/navigation.module'、'common/topbar/topbar.module'、'common/general/general.module'、'app/y/topology.module'、'app/yangui/main'、'common/yangutils/yangutils.module'、'common/sigmotopology/sigmotopology.module'、'app/yangvisualizer/yangvisualizer.module'、'common/layout/layout.module';
变量e=['oc.lazyLoad'、'ui.router'、'pascalprecht.translate'、'ngSanitize'、'angular.css.injector'、'app.core'、'app.designboard'、'app.testautomation'、'app.common.login'、'app.common.auth'、'app.common.topbar'、'app.common.general'、'app.topology'、'app.yangUtils'、'app.common.sigmologysualizer','app.common.layout'];
//全局变量

在我看来,您所观察到的错误类型似乎是由
Selenium
geckodriver
Firefox
版本之间的不匹配引起的

根据发行说明
Selenium v3.6.0
应与
geckodriver v.0.18.0
配合使用(推荐使用As
geckodriver v.0.19.0

但是
geckodriver v.0.18.0
发行说明明确提到以下内容:

因此,将您的
Firefox
升级到
v53+
级别肯定会解决您的问题

更新: 根据您的评论,您可以将
username
字段的定位器更改为以下任一项:

  • css\u选择器

    driver.find_element_by_css_selector("input[name=username]")
    
    driver.find_element_by_xpath("//input[@name='username']")
    
  • xpath

    driver.find_element_by_css_selector("input[name=username]")
    
    driver.find_element_by_xpath("//input[@name='username']")
    

  • 很可能是您的驱动程序在第一次加载后试图获取用户名字段,但由于出现异常而失败,并在重定向之前将其关闭。它需要等待。您可以尝试:

    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    ...
    def test_worked2(self):
        driver = self.driver
        driver.get(base_url)
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.NAME, 'username'))).send_keys("xxx")
    

    我通过使用Firefox配置文件解决了这个问题,我不知道它有什么不同,但它确实起到了作用

    创建Firefox配置文件的链接

    from selenium import webdriver
    fp = webdriver.FirefoxProfile('C:/Users/<user name>/AppData/Roaming/Mozilla/Firefox/Profiles/abc3defghij2.ProfileName')
    driver = webdriver.Firefox(fp)
    driver.get("https://www.example.com/membersarea")
    

    下面是代码

    from selenium import webdriver
    fp = webdriver.FirefoxProfile('C:/Users/<user name>/AppData/Roaming/Mozilla/Firefox/Profiles/abc3defghij2.ProfileName')
    driver = webdriver.Firefox(fp)
    driver.get("https://www.example.com/membersarea")
    
    从selenium导入webdriver
    fp=webdriver.FirefoxProfile('C:/Users//AppData/Roaming/Mozilla/Firefox/Profiles/abc3defghij2.ProfileName')
    driver=webdriver.Firefox(fp)
    驱动程序。获取(“https://www.example.com/membersarea")
    
    更正driver.get(self.base_url+“/index.html#/login”)中的url,因为安装程序的基本url也有“/index.html#/login”@坏蛋的问题仍然是一样的问题可能是重点不在弹出窗口上,因为登录元素出现在弹出窗口上,你能分享网站的html吗?@坏蛋请看编辑我把我的firefox升级到56,我仍然面临同样的错误..更新我的日志不,它们是一样的,我认为可能会有一些变化。所以你a同时引用旧的元素参见您看到的第一个集合
    NoSuchElementException:Message:无法定位元素:[name=“username”]
    在第二组中,您看到的是
    WebDriverException:Message:quit
    。您的确切问题是什么?我删除了quit错误以避免混淆。我的问题是,经过一段时间测试用例失败并出现“无法定位”错误后,网站无法完全加载。我希望我的url完全加载,但您的IP是ClassA IP,因此无法查看网站是否存在页面加载问题以及如何解决。