This function takes a selenium browser and a css selector string and uses them to find the target HTML image element. The desired image element should contain it's image data as a Base64 encoded JPEG image string. The 'src' attribute of the image is read, Base64-decoded, and then
(browser, cssSelector)
| 521 | # then base64 decode the "src" attribute and return it. |
| 522 | # ============================================================================= |
| 523 | def get_image_data(browser, cssSelector): |
| 524 | """ |
| 525 | This function takes a selenium browser and a css selector string and uses |
| 526 | them to find the target HTML image element. The desired image element |
| 527 | should contain it's image data as a Base64 encoded JPEG image string. |
| 528 | The 'src' attribute of the image is read, Base64-decoded, and then |
| 529 | returned. |
| 530 | |
| 531 | browser: A selenium browser instance, as created by webdriver.Chrome(), |
| 532 | for example. |
| 533 | |
| 534 | cssSelector: A string containing a CSS selector which will be used to |
| 535 | find the HTML image element of interest. |
| 536 | """ |
| 537 | |
| 538 | # Here's maybe a better way to get at that image element |
| 539 | imageElt = browser.find_element_by_css_selector(cssSelector) |
| 540 | |
| 541 | # Now get the Base64 image string and decode it into image data |
| 542 | base64String = imageElt.get_attribute("src") |
| 543 | b64RegEx = re.compile(r"data:image/jpeg;base64,(.+)") |
| 544 | b64Matcher = b64RegEx.match(base64String) |
| 545 | imgdata = base64.b64decode(b64Matcher.group(1)) |
| 546 | |
| 547 | return imgdata |
| 548 | |
| 549 | |
| 550 | # ============================================================================= |