• python selenium 问题汇总


    FAQ

    1.python+selenium+Safari浏览器,定位元素

    selenium.common.exceptions.ElementNotVisibleException: Message: An element command could not be completed because the element is not visible on the page.
    在GitHub上有人提出过同样的问题,https://github.com/SeleniumHQ/selenium/issues/5355,有一个回答如下:

    可以参考https://bugs.webkit.org/show_bug.cgi?id=174710,这个bug在Safari开发版已经修复,但并未发布。
    那么怎么解决Safari浏览器的点击问题呢?

    解决方法如下:
    通过调用js的方法进行操作

    execute_script(script, *args)
    #Example:
    ele = browser.find_element_by_xpath('//*[@id="1$Menu"]/li')
    browser.execute_script('arguments[0].click();', ele)
    

    2.python3+selenium 3.13 + geckodriver 21.0,提示ConnectionResetError,切换为较低版本的driver即可

    学习selenium时,如果sleep时间大于等于5秒,就会提示ConnectionResetError: [Errno 54] Connection reset by peer。
    换成chrome浏览器,可以正常运行。

    demo.py

    from selenium import webdriver
    from time import sleep, ctime
    #chrome
    #driver = webdriver.Chrome(executable_path='//Users/csj/Desktop/seleniumdriver/chromedriver/chromedriver')
    #Firefoxdriver 21.0
    driver = webdriver.Firefox(executable_path='//Users/csj/Desktop/seleniumdriver/firefoxdriver/21.0/geckodriver')
    #Firefoxdriver 20.1
    #driver = webdriver.Firefox(executable_path='//Users/csj/Desktop/seleniumdriver/firefoxdriver/20.1/geckodriver')
    #Firefoxdriver 20.0
    #driver = webdriver.Firefox(executable_path='//Users/csj/Desktop/seleniumdriver/firefoxdriver/20.0/geckodriver')
    #Firefox 19.1
    #driver = webdriver.Firefox(executable_path='//Users/csj/Desktop/seleniumdriver/firefoxdriver/19.1/geckodriver')
    driver.implicitly_wait(10)
    driver.get("http://www.baidu.com")
    print(ctime())
    sleep(5)
    print(ctime())
    driver.find_element_by_id("kw").send_keys("selenium")
    driver.close()
    

    使用Firefoxdriver 21.0,报如下错误:

    Traceback (most recent call last):
      File "/Users/csj/PycharmProjects/untitled/demo.py", line 13, in <module>
        driver.find_element_by_id("kw").send_keys("selenium")
      File "/Users/csj/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 353, in find_element_by_id
        return self.find_element(by=By.ID, value=id_)
      File "/Users/csj/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 957, in find_element
        'value': value})['value']
      File "/Users/csj/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
        response = self.command_executor.execute(driver_command, params)
      File "/Users/csj/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 472, in execute
        return self._request(command_info[0], url, body=data)
      File "/Users/csj/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 496, in _request
        resp = self._conn.getresponse()
      File "/Users/csj/anaconda/lib/python3.6/http/client.py", line 1331, in getresponse
        response.begin()
      File "/Users/csj/anaconda/lib/python3.6/http/client.py", line 297, in begin
        version, status, reason = self._read_status()
      File "/Users/csj/anaconda/lib/python3.6/http/client.py", line 258, in _read_status
        line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
      File "/Users/csj/anaconda/lib/python3.6/socket.py", line 586, in readinto
        return self._sock.recv_into(b)
    ConnectionResetError: [Errno 54] Connection reset by peer
    

    换成chrome,可以正常运行。老板提醒可能是版本的问题,分别用 Firefox 20.1, 20.0, 19.1,均能运行成功,没有再提示ConnectionResetError。
    休眠时间设置为10秒,也没有再报错。

  • 相关阅读:
    Java技术路线--2循环
    Java技术路线--1基本类型与包装类
    Linux内存管理之UMA模型和NUMA模型
    最长XX子串/数组/子序列
    epoll LT 模式和 ET 模式详解
    OS篇:OS中进程的阻塞与挂起的区别
    约瑟夫环问题
    最大公约数和最小公倍数
    C++之寻找素数(素数筛)
    Linux OOM机制分析
  • 原文地址:https://www.cnblogs.com/csj2018/p/9790898.html
Copyright © 2020-2023  润新知