Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.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 Selenium在向下滚动页面中捕获验证码_Python_Selenium - Fatal编程技术网

Python Selenium在向下滚动页面中捕获验证码

Python Selenium在向下滚动页面中捕获验证码,python,selenium,Python,Selenium,我想用python和selenium捕获captcha代码。到目前为止,我有: elem = driver.find_element_by_css_selector("#imagecpt") loc, size = elem.location, elem.size left, top = loc['x'], (loc['y'] - 587) width, height = size['width'], size['height'] box = (int(left), int(top),

我想用python和selenium捕获captcha代码。到目前为止,我有:

elem = driver.find_element_by_css_selector("#imagecpt")
loc, size  = elem.location, elem.size
left, top     = loc['x'], (loc['y'] - 587)
width, height = size['width'], size['height']
box = (int(left), int(top), int(left+width), int(top+height))
screenshot = driver.get_screenshot_as_base64()
img = Image.open(StringIO.StringIO(base64.b64decode(screenshot)))
captcha = img.crop(box)
captcha.save('captcha.png', 'PNG')
代码正在运行,但有一个小问题,
elem.location返回的顶部位置是网站中的完整位置

要在我的网站中捕获captcha代码,我需要向下滚动页面,因为Selenium只对网站的可见部分截图,并且由
elem返回的顶部大小。位置
无效


为了解决滚动问题,我使用了一个硬编码值
-587
。如果没有此硬编码值,如何重写代码?

使用
ActionChains。将\u移动到\u element()
方法滚动到验证码

另外,请查看Selenium文档中的,因为它可能会有所帮助

此属性可能会在没有警告的情况下更改。用这个来发现在哪里 在屏幕上显示一个元素,以便我们可以单击它。这种方法 应使元素滚动到视图中

返回屏幕上左上角的位置,如果 元素不可见

from selenium.webdriver import ActionChains

elem = driver.find_element_by_css_selector("#imagecpt")

action_chain = ActionChains(driver)
action_chain.move_to_element(elem)
action_chain.perform()

loc, size = elem.location_once_scrolled_into_view, elem.size
left, top = loc['x'], loc['y']

width, height = size['width'], size['height']
box = (int(left), int(top), int(left + width), int(top + height))

screenshot = driver.get_screenshot_as_base64()
img = Image.open(StringIO.StringIO(base64.b64decode(screenshot)))

captcha = img.crop(box)
captcha.save('captcha.png', 'PNG')