生于理想,死于欲望
分析过程,虽然没啥用的
相信很多朋友都遇到过这个问题,以下是一个分析总结,虽然没多大用,哈哈
最终的解决方法在结尾,并加入另一位博主的解决办法https://blog.csdn.net/qq_47729488/article/details/119357291
以下是我的代码
class ALITBSpider(object):
def __init__(self,service_ip,username,password):
self.path = "H:\python-练习\爬虫\chromedriver_win32\chromedriver.exe"
self.options = webdriver.ChromeOptions()
self.headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",}
self.driver= webdriver.Chrome(executable_path=self.path, options=self.options)
self.driver.get("https://www.taobao.com")
self.num = 0
def login(self,username=None,password=None):
'''密码登录'''
self.driver.implicitly_wait(3)
if self.driver.find_element_by_link_text("亲,请登录"):
self.driver.find_element_by_link_text("亲,请登录").click()
self.driver.find_elements_by_id("fm-login-id")[0].send_keys(username)
self.driver.find_elements_by_id("fm-login-password")[0].send_keys(password)
auth_code = WebDriverWait(self.driver,10,1.5).until(
lambda code :self.driver.find_element_by_class_name("nc_iconfont") #滑动验证
)
if auth_code:
self.driver.implicitly_wait(3)
actions = ActionChains(self.driver)
actions.click_and_hold(auth_code) #按住鼠标左键
actions.pause(1)
actions.move_by_offset(258,258) #鼠标拖动
actions.pause(1)
actions.release() #释放鼠标
actions.perform() #执行操作
self.driver.implicitly_wait(3) #显示等待
self.driver.find_element_by_class_name("password-login").click() #登陆
selenium.common.exceptions.JavascriptException: Message: javascript error: Failed to execute ‘elementsFromPoint’ on ‘Document’: The provided double value is non-finite.
运行报错,人工重试也报错
我们先找到这个的源码
def perform(self):
"""
Performs all stored actions.
"""
if self._driver.w3c:
self.w3c_actions.perform()
else:
for action in self._actions:
action()
接着找到他执行的语句
def perform(self):
enc = {"actions": []}
for device in self.devices:
encoded = device.encode()
if encoded['actions']:
enc["actions"].append(encoded)
print(enc)
self.driver.execute(Command.W3C_ACTIONS, enc)
发现,命令存在的,一个指针, 2个移动指针,2个等待的,但还是报错
错误来源
发现
response 如下
{'value': None}
{'value': {'element-6066-11e4-a52e-4f735466cecf': '1a669d6f-f2cf-41ac-862a-d281230e1a1b'}}
{'value': {'element-6066-11e4-a52e-4f735466cecf': '1a669d6f-f2cf-41ac-862a-d281230e1a1b'}}
{'value': None}
{'value': [{'element-6066-11e4-a52e-4f735466cecf': '179ddb7d-9ae5-44d0-b14c-832bb1c79bc1'}]}
{'value': None}
{'value': [{'element-6066-11e4-a52e-4f735466cecf': 'de8a391b-918c-4b09-8dab-26fb0a519282'}]}
{'value': None}
{'value': {'element-6066-11e4-a52e-4f735466cecf': 'af59e205-5394-4548-bbc9-685c0441497b'}}
{'value': None}
{'actions': [{'type': 'pointer', 'parameters': {'pointerType': 'mouse'}, 'id': 'mouse', 'actions': [{'type': 'pointerMove', 'duration': 250, 'x': 0, 'y': 0, 'origin': {'element-6066-11e4-a52e-4f735466cecf': 'af59e205-5394-4548-bbc9-685c0441497b'}}, {'type': 'pointerDown', 'duration': 0, 'button': 0}, {'type': 'pause', 'duration': 1000}, {'type': 'pointerMove', 'duration': 250, 'x': 258, 'y': 258, 'origin': 'pointer'}, {'type': 'pause', 'duration': 1000}, {'type': 'pointerUp', 'duration': 0, 'button': 0}]}, {'type': 'key', 'id': 'key', 'actions': [{'type': 'pause', 'duration': 0}, {'type': 'pause', 'duration': 0}, {'type': 'pause', 'duration': 1000}, {'type': 'pause', 'duration': 0}, {'type': 'pause', 'duration': 1000}, {'type': 'pause', 'duration': 0}]}]}
{'status': 500, 'value': '{"value":{"error":"javascript error","message":"javascript error: Failed to execute \'elementsFromPoint\' on
\'Document\': The provided double value is non-finite.\\n (Session info: chrome=85.0.4183.121)","stacktrace":"Backtrace:\\n\\tOrdinal0 [0x00EAD383+3134339]\\n\\tOrdinal0 [0x00D9A171+2007409]\\n\\tOrdinal0 [0x00C3AEE8+569064]\\n\\tOrdinal0 [0x00C3D08E+577678]\\n\\tOrdinal0 [0x00C3CF93+577427]\\n\\tOrdinal0 [0x00C3D170+577904]\\n\\tOrdinal0 [0x00BCDB2C+121644]\\n\\tOrdinal0 [0x00BCD5B1+120241]\\n\\tOrdinal0 [0x00BED657+251479]\\n\\tOrdinal0 [0x00BDEC5D+191581]\\n\\tOrdinal0 [0x00BE8773+231283]\\n\\tOrdinal0 [0x00BDEB0B+191243]\\n\\tOrdinal0 [0x00BC2E77+77431]\\n\\tOrdinal0 [0x00BC3E3E+81470]\\n\\tOrdinal0 [0x00BC3DC9+81353]\\n\\tOrdinal0 [0x00DB0CD9+2100441]\\n\\tGetHandleVerifier [0x0101B75A+1396954]\\n\\tGetHandleVerifier [0x0101B3D9+1396057]\\n\\tGetHandleVerifier [0x01027126+1444518]\\n\\tGetHandleVerifier [0x0101BCE8+1398376]\\n\\tOrdinal0 [0x00DA7F51+2064209]\\n\\tOrdinal0 [0x00DB22EB+2106091]\\n\\tOrdinal0 [0x00DB2411+2106385]\\n\\tOrdinal0 [0x00DC49C4+2181572]\\n\\tBaseThreadInitThunk [0x7683FA29+25]\\n\\tRtlGetAppContainerNamedObjectPath [0x778C75F4+228]\\n\\tRtlGetAppContainerNamedObjectPath [0x778C75C4+180]\\n"}}'}
执行命令失败
最终解决方法
最后从前端入手
大多数都是检测navigator,因此
在开头加入
self.options.add_argument("disable-blink-features=AutomationControlled")
和在请求前加入这个
#添加协议IP
self.options.add_argument('...proxy_server='+random.choice(proxies))
#防止淘宝检测到selenium登录
self.options.add_experimental_option('excludeSwitches', ['enable-automation'])
self.options.add_experimental_option('useAutomationExtension', False)
self.driver.execute_cdp_cmd(
"Page.addScriptToEvaluateOnNewDocument",{
"source":'''
Object.defineProperty(navigator,'webdriver',{
get: () => undefined
})
'''
}
)
self.driver.implicitly_wait(3) #显式等待
完整代码:
class ALITBSpider(object):
def __init__(self,service_ip,username,password):
self.path = "H:\python-练习\爬虫\chromedriver_win32\chromedriver.exe"
self.options = webdriver.ChromeOptions()
self.options.add_argument("disable-blink-features=AutomationControlled")
self.headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",}
self.driver= webdriver.Chrome(executable_path=self.path, options=self.options)
self.driver.get("https://www.taobao.com")
self.num = 0
self.create_table()
def login(self,username=None,password=None):
'''密码登录'''
self.driver.implicitly_wait(3)
if self.driver.find_element_by_link_text("亲,请登录"):
self.driver.find_element_by_link_text("亲,请登录").click()
self.driver.find_elements_by_id("fm-login-id")[0].send_keys(username)
self.driver.find_elements_by_id("fm-login-password")[0].send_keys(password)
auth_code = WebDriverWait(self.driver,10,1.5).until(
lambda code :self.driver.find_element_by_class_name("nc_iconfont") #滑动验证
)
# if auth_code:
# self.driver.implicitly_wait(3)
# actions = ActionChains(self.driver)
# actions.click_and_hold(auth_code) #按住鼠标左键
# actions.pause(1)
# actions.move_by_offset(258,258) #鼠标拖动
# actions.pause(1)
# actions.release() #释放鼠标
# actions.perform() #执行操作
# actions.reset_actions() #清空
#添加协议IP
self.options.add_argument('...proxy_server='+random.choice(proxies))
#防止淘宝检测到selenium登录
self.options.add_experimental_option('excludeSwitches', ['enable-automation'])
self.options.add_experimental_option('useAutomationExtension', False)
self.driver.execute_cdp_cmd(
"Page.addScriptToEvaluateOnNewDocument",{
"source":'''
Object.defineProperty(navigator,'webdriver',{
get: () => undefined
})
'''
}
)
self.driver.implicitly_wait(3) #显式等待
self.driver.find_element_by_class_name("password-login").click() #登陆
登陆成功,网页中的滑动验证,及一次常用的也是用相同的办法