python selenium 问题汇总

本文探讨了使用Python+selenium进行网页自动化时遇到的两大常见问题:一是Safari浏览器元素定位时的可见性异常,解决方案是通过执行JavaScript来触发元素;二是使用geckodriver时出现的ConnectionResetError,降低driver版本可以避免此问题。
摘要由CSDN通过智能技术生成

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,有一个回答如下:
1418970-20181015143502830-753700317.png
可以参考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秒,也没有再报错。

转载于:https://www.cnblogs.com/csj2018/p/9790898.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值