www.91084.com

GVKun编程网logo

Selenium WebDriver通过Python3.5和FF48发生“无法加载配置文件”错误(from selenium import webdriver报错)

7

对于想了解SeleniumWebDriver通过Python3.5和FF48发生“无法加载配置文件”错误的读者,本文将是一篇不可错过的文章,我们将详细介绍fromseleniumimportwebdr

对于想了解Selenium WebDriver通过Python3.5和FF48发生“无法加载配置文件”错误的读者,本文将是一篇不可错过的文章,我们将详细介绍from selenium import webdriver报错,并且为您提供关于Python + Selenium WebDriver Api 知识回顾、python + selenium WebDriver的环境配置、Python Selenium Chrome Webdriver、python selenium webdriver chrome的有价值信息。

本文目录一览:

Selenium WebDriver通过Python3.5和FF48发生“无法加载配置文件”错误(from selenium import webdriver报错)

Selenium WebDriver通过Python3.5和FF48发生“无法加载配置文件”错误(from selenium import webdriver报错)

我正在尝试将Selenium与Python结合使用。
因此,我编写了以下代码,并将其保存为工作目录 / Users / ykt68 / seleniumwork中 名为 test.py 的文件。


[ykt68@macbp15 seleniumwork]$ pwd/Users/ykt68/seleniumwork[ykt68@macbp15 seleniumwork]$ cat -n test.py
         1  #! /usr/bin/env python         2  # -*- encoding: utf-8 -*-         3           4  from selenium import webdriver         5  from selenium.webdriver.common.keys import Keys         6           7  driver = webdriver.Firefox()         8  driver.get("http://www.python.org")         9  assert "Python" in driver.title        10  elem = driver.find_element_by_name("q")        11  elem.clear()        12  elem.send_keys("pycon")        13  elem.send_keys(Keys.RETURN)        14  assert "No results found." not in driver.page_source        15  driver.close()
[ykt68@macbp15 seleniumwork]$

上面的这些代码与使用Python的Selenium文档中的2.1简单用法相同。

当我为上面的test.py运行python命令时,

  • FireFox浏览器启动并打开一个空白选项卡。
  • 大约30秒过后,将显示以下错误消息,并关闭FireFox窗口。
    [ykt68@macbp15 seleniumwork]$ python test.py     Traceback (most recent call last):      File "test.py", line 7, in <module>        driver = webdriver.Firefox()      File "/Users/ykt68/.pyenv/versions/seleniumwork/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 80, in __init__        self.binary, timeout)      File "/Users/ykt68/.pyenv/versions/seleniumwork/lib/python3.5/site-packages/selenium/webdriver/firefox/extension_connection.py", line 52, in    __init__        self.binary.launch_browser(self.profile, timeout=timeout)      File "/Users/ykt68/.pyenv/versions/seleniumwork/lib/python3.5/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser        self._wait_until_connectable(timeout=timeout)      File "/Users/ykt68/.pyenv/versions/seleniumwork/lib/python3.5/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 108, in _wait_until_connectable        % (self.profile.path))    selenium.common.exceptions.WebDriverException: Message: Can''t load the profile. Profile Dir: /var/folders/86/55p1gj4j4xz2nw9g5q224bk40000gn/T/tmpf0uolidn If you specified a log_file in the FirefoxBinary constructor, check it for details.    [ykt68@macbp15 seleniumwork]$

请教我为什么会发生此错误,以及如何解决该问题,或者列出一些我应该参考的帖子或文件。

此外,

  1. 环境:
    • 操作系统:Apple OS X Version 10.11.6
    • Python版本:3.5.2
    • FireFox版本:48.0.2
    • selenium版本:2.53.6
    [ykt68@macbp15 seleniumwork]$ python -V    Python 3.5.2    [ykt68@macbp15 seleniumwork]$ /Applications/Firefox.app/Contents/MacOS/firefox -v    Mozilla Firefox 48.0.2    [ykt68@macbp15 seleniumwork]$ /Applications/Firefox.app/Contents/MacOS/firefox-bin -v    Mozilla Firefox 48.0.2    [ykt68@macbp15 seleniumwork]$ pip list    pip (8.1.2)    selenium (2.53.6)    setuptools (20.10.1)    [ykt68@macbp15 seleniumwork]$
  1. 我引用了Selenium的类似文章 :FirefoxProfile异常无法加载配置文件。所以我尝试了

    pip install -U selenium

但是上面带有错误消息的情况保持不变。

最好的祝福。

答案1

小编典典

据我了解和得出的结论,您可以保留最新的selenium软件包版本,但是必须 将Firefox降级为47
(47.0.1是47分支的最新稳定版本)。

Python + Selenium WebDriver Api 知识回顾

Python + Selenium WebDriver Api 知识回顾

 

一直再用 Selenium WebDriver 但是用的都比较零散,也没有做过总结,今天借此机会,整理一下,方便大家使用时查阅

 

webDriver  的属性

[''CONTEXT_CHROME'', ''CONTEXT_CONTENT'', ''NATIVE_EVENTS_ALLOWED'', ''__class__'', ''__delattr__'', ''__dict__'', ''__dir__'', ''__doc__'', ''__eq__'', ''__format__'', ''__ge__'', ''__getattribute__'', ''__gt__'', 

''__hash__'', ''__init__'', ''__init_subclass__'', ''__le__'', ''__lt__'', ''__module__'', ''__ne__'', ''__new__'', ''__reduce__'', ''__reduce_ex__'', ''__repr__'', ''__setattr__'', ''__sizeof__'', ''__str__'',

''__subclasshook__'', ''__weakref__'', ''_file_detector'', ''_is_remote'', ''_mobile'', ''_switch_to'', ''_unwrap_value'', ''_web_element_cls'', ''_wrap_value'', ''add_cookie'', ''application_cache'',

''back'', ''binary'', ''capabilities'', ''close'', ''command_executor'', ''context'', ''create_web_element'', ''current_url'', ''current_window_handle'', ''delete_all_cookies'', ''delete_cookie'',

''desired_capabilities'', ''error_handler'', ''execute'', ''execute_async_script'', ''execute_script'', ''file_detector'', ''file_detector_context'', ''find_element'', ''find_element_by_class_name'',

''find_element_by_css_selector'', ''find_element_by_id'', ''find_element_by_link_text'', ''find_element_by_name'', ''find_element_by_partial_link_text'', ''find_element_by_tag_name'',

''find_element_by_xpath'', ''find_elements'', ''find_elements_by_class_name'', ''find_elements_by_css_selector'', ''find_elements_by_id'', ''find_elements_by_link_text'',

''find_elements_by_name'', ''find_elements_by_partial_link_text'', ''find_elements_by_tag_name'', ''find_elements_by_xpath'', ''firefox_profile'', ''forward'', ''fullscreen_window'',

''get'', ''get_cookie'', ''get_cookies'', ''get_log'', ''get_screenshot_as_base64'', ''get_screenshot_as_file'', ''get_screenshot_as_png'', ''get_window_position'', ''get_window_rect'',

''get_window_size'', ''implicitly_wait'', ''install_addon'', ''log_types'', ''maximize_window'', ''minimize_window'', ''mobile'', ''name'', ''orientation'', ''page_source'', ''profile'',

''quit'', ''refresh'', ''save_screenshot'', ''service'', ''session_id'', ''set_context'', ''set_page_load_timeout'', ''set_script_timeout'', ''set_window_position'', ''set_window_rect'',

''set_window_size'', ''start_client'', ''start_session'', ''stop_client'', ''switch_to'', ''switch_to_active_element'', ''switch_to_alert'', ''switch_to_default_content'',

''switch_to_frame'', ''switch_to_window'', ''title'', ''uninstall_addon'', ''w3c'', ''window_handles'']

 

调用说明:

driver.属性值

 

变量说明:

 

# FileName : WebDriverApi.py
# Author   : Adil
# DateTime : 2018/3/12 11:40
# SoftWare : PyCharm

from selenium import webdriver

import time
driver = webdriver.Firefox()
# driver.implicitly_wait(1)
driver.get("http://www.baidu.com/")

print(dir(driver))                  # driver attributes

print(driver.current_url)           # 用于获得当前页面的URL  https://www.baidu.com/index.php?tn=56060048_5_pg&ch=6

print(driver.current_window_handle) # 用于获取当前窗口句柄 2147483651

print(driver.window_handles)        # 用于获取所有窗口句柄 [''2147483651'']

print(driver.title)                 # 用于获取当前页面的标题  百度一下,你就知道

print(driver.page_source)           # 用于获取页面html源代码

time.sleep(5)
# driver.implicitly_wait(10)
print("element")
elem = driver.find_element_by_id(''kw'')


print(dir(elem))

driver.quit()

 

函数说明:
1.driver.find_element*():定位元素,详看另外一篇博文:Selenuim+Python之元素定位总结及实例说明
2.driver.get(url):浏览器加载url。
实例:driver.get("http//:www.baidu.com")
3.driver.forward():浏览器向前(点击向前按钮)。
4.driver.back():浏览器向后(点击向后按钮)。
5.driver.refresh():浏览器刷新(点击刷新按钮)。
6.driver.close():关闭当前窗口,或最后打开的窗口。
7.driver.quit():关闭所有关联窗口,并且安全关闭session。
8.driver.maximize_window():最大化浏览器窗口。
9.driver.set_window_size(宽,高):设置浏览器窗口大小。
10.driver.get_window_size():获取当前窗口的长和宽。
11.driver.get_window_position():获取当前窗口坐标。
12.driver.get_screenshot_as_file(filename):截取当前窗口。
实例:driver.get_screenshot_as_file(''D:/selenium/image/baidu.jpg'')
13.driver.implicitly_wait(秒):隐式等待,通过一定的时长等待页面上某一元素加载完成。
若提前定位到元素,则继续执行。若超过时间未加载出,则抛出NoSuchElementException异常。
实例:driver.implicitly_wait(10) #等待10秒
14.driver.switch_to_frame(id或name属性值):切换到新表单(同一窗口)。若无id或属性值,可先通过xpath定位到iframe,再将值传给switch_to_frame()
15.driver.switch_to.parent_content():跳出当前一级表单。该方法默认对应于离它最近的switch_to.frame()方法。
16.driver.switch_to.default_content():跳回最外层的页面。
17.driver.switch_to_window(窗口句柄):切换到新窗口。
18.driver.switch_to.window(窗口句柄):切换到新窗口。
19.driver.switch_to_alert():警告框处理。处理JavaScript所生成的alert,confirm,prompt.
20.driver.switch_to.alert():警告框处理。
21.driver.execute_script(js):调用js。
22.driver.get_cookies():获取当前会话所有cookie信息。
23.driver.get_cookie(cookie_name):返回字典的key为“cookie_name”的cookie信息。
实例:driver.get_cookie("NET_SessionId")
24.driver.add_cookie(cookie_dict):添加cookie。“cookie_dict”指字典对象,必须有name和value值。
25.driver.delete_cookie(name,optionsString):删除cookie信息。
26.driver.delete_all_cookies():删除所有cookie信息。

 

 

页面元素属性

print(dir(elem))

 

 

[''__class__'', ''__delattr__'', ''__dict__'', ''__dir__'', ''__doc__'', ''__eq__'', ''__format__'', ''__ge__'', ''__getattribute__'', ''__gt__'', ''__hash__'', ''__init__'', ''__init_subclass__'', 

''__le__'', ''__lt__'', ''__module__'', ''__ne__'', ''__new__'', ''__reduce__'', ''__reduce_ex__'', ''__repr__'', ''__setattr__'', ''__sizeof__'', ''__str__'', ''__subclasshook__'',

''__weakref__'', ''_execute'', ''_id'', ''_parent'', ''_upload'', ''_w3c'', ''anonymous_children'', ''clear'', ''click'', ''find_anonymous_element_by_attribute'', ''find_element'',

''find_element_by_class_name'', ''find_element_by_css_selector'', ''find_element_by_id'', ''find_element_by_link_text'', ''find_element_by_name'', ''find_element_by_partial_link_text'',

''find_element_by_tag_name'', ''find_element_by_xpath'', ''find_elements'', ''find_elements_by_class_name'', ''find_elements_by_css_selector'', ''find_elements_by_id'',

''find_elements_by_link_text'', ''find_elements_by_name'', ''find_elements_by_partial_link_text'', ''find_elements_by_tag_name'', ''find_elements_by_xpath'', ''get_attribute'',

''get_property'', ''id'', ''is_displayed'', ''is_enabled'', ''is_selected'', ''location'', ''location_once_scrolled_into_view'', ''parent'', ''rect'', ''screenshot'', ''screenshot_as_base64'',

''screenshot_as_png'', ''send_keys'', ''size'', ''submit'', ''tag_name'', ''text'', ''value_of_css_property'']

 

 

调用说明:
driver.find_element*.属性值

element=driver.find_element*
element.属性值

变量说明:
1.element.size:获取元素的尺寸。
2.element.text:获取元素的文本。
3.element.tag_name:获取标签名称。

函数说明:
1.element.clear():清除文本。
2.element.send_keys(value):输入文字或键盘按键(需导入Keys模块)。
3.element.click():单击元素。
4.element.get_attribute(name):获得属性值
5.element.is_displayed():返回元素结果是否可见(True 或 False)
6.element.is_selected():返回元素结果是否被选中(True 或 False)
7.element.find_element*():定位元素,用于二次定位。

 

python + selenium WebDriver的环境配置

python + selenium WebDriver的环境配置

     想试用python语言来学习selenium WebDriver,首先需要搭建一个测试环境,从python安装到浏览器插件配置的详细步骤,总结如下:

一、python环境配置

      1、从官网下载最新的一个python包https://www.python.org/downloads

      2、python安装选择自定义安装,方便配置环境变量,其他安装步骤和其他软件安装一致

      

 

    3、安装成功后去配置环境变量  

     

 

4、去验证python的环境配置是否成功,打开cmd窗口,输入python,如果环境配置成功会出现安装的python版本信息

  

 

   5、在python的安装目录下找到Scripts文件下的pip.exe,然后把该路径配置到环境变量中,如果没有该文件,需要单独去下载安装

 

   配置成功后,在cmd窗口,输入pip,会出现如下信息

 

 

二、使用pip工具安装Selenium

  1、进入pip所在安装目录执行下面命令可以在线安装

           D:\Python\Scripts>pip install -U selenium

 

三、下载webdriver插件

    1、chrome浏览器的webdriver下载地址 http://chromedriver.storage.googleapis.com/index.html ,需要找到浏览器版本对应的插件

    2、把解压后的文件放在python文件下,和python.exe同一目录下

   

   3、浏览器的安装路径也需要加入环境变量

 

 

 

四、验证webdriver插件环境是否配置成功

 1、运行python.exe

  from selenium import webdriver

brower=webdriver.Chrome()  //能打开浏览器说明配置成功了

 

 

 

 

 

  

 

 

 

  

 

 

 

 

 

 

       

Python Selenium Chrome Webdriver

Python Selenium Chrome Webdriver

我开始自动化无聊的东西书,并且尝试通过python打开chrome网络浏览器。我已经安装了selenium和

我试图运行此文件:

from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysbrowser = webdriver.Chrome()browser.get(''https://automatetheboringstuff.com'')

但是正因为如此,我得到这个错误:

Traceback (most recent call last):   File "C:\Program Files   (x86)\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start     stdout=self.log_file, stderr=self.log_file)   File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 707, in __init__     restore_signals, start_new_session)   File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 990, in _execute_child     startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified

在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):   File "C:/Program Files(x86)/Python36-32/test.py", line 5, in <module>    browser = webdriver.Chrome()   File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py",line 62, in __init__   self.service.start()   File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\common\service.py",line 81, in start   os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: ''chromedriver''  executable needs to be in PATH. Please seehttps://sites.google.com/a/chromium.org/chromedriver/home

答案1

小编典典

您需要 指定chromedriver所在的路径

  1. 从此处下载适用于所需平台的chromedriver。

  2. 将chromedriver放置在系统路径或代码所在的位置。

  3. 如果不使用系统路径,请链接您的chromedriver.exe(对于非Windows用户,简称为chromedriver):

    browser = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe")

(设置executable_path为chromedriver所在的位置。)

如果您在系统路径上放置了chromedriver,则可以通过以下操作来实现快捷方式:

browser = webdriver.Chrome()

  1. 如果您在基于Unix的操作系统上运行,则可能需要在下载chromedriver后更新chromedriver的权限,以使其可执行:

chmod +x chromedriver

python selenium webdriver chrome

python selenium webdriver chrome

如何解决python selenium webdriver chrome?

from selenium import webdriver

d = webdriver.Chrome("/usr/bin/chromedriver")

d.get("https://google.co.in")


error:
[Running] python -u "/home/nourhene/py/hi.py"
Traceback (most recent call last):
  File "/home/nourhene/py/hi.py",line 11,in <module>
    d = webdriver.Chrome("/usr/bin/chromedriver")
  File "/home/nourhene/.local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py",line 81,in __init__
    desired_capabilities=desired_capabilities)
  File "/home/nourhene/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",line 157,in __init__
    self.start_session(capabilities,browser_profile)
  File "/home/nourhene/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",line 252,in start_session
    response = self.execute(Command.NEW_SESSION,parameters)
  File "/home/nourhene/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",line 321,in execute
    self.error_handler.check_response(response)
  File "/home/nourhene/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py",line 242,in check_response
    raise exception_class(message,screen,stacktrace)
selenium.common.exceptions.WebDriverException: Message: unkNown error: Chrome Failed to start: crashed
  (Driver info: chromedriver=2.26.436382 (70eb799287ce4c2208441fc057053a5b07ceabac),platform=Linux 4.4.0-19041-Microsoft x86_64)

解决方法

您使用的是 Windows,所以应该是:

/chromedriver.exe

代替:

/chromedriver

我们今天的关于Selenium WebDriver通过Python3.5和FF48发生“无法加载配置文件”错误from selenium import webdriver报错的分享就到这里,谢谢您的阅读,如果想了解更多关于Python + Selenium WebDriver Api 知识回顾、python + selenium WebDriver的环境配置、Python Selenium Chrome Webdriver、python selenium webdriver chrome的相关信息,可以在本站进行搜索。

本文标签: