Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to selenium 4 and fixed indices #42

Open
wants to merge 1 commit into
base: patch-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions google_images_download/google_images_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Import Libraries
import sys
import selenium.common.exceptions
from selenium.webdriver.common.by import By

version = (3, 0)
cur_version = sys.version_info
Expand Down Expand Up @@ -43,7 +44,7 @@
"exact_size", "aspect_ratio", "type", "time", "time_range", "delay", "url", "single_image",
"output_directory", "image_directory", "no_directory", "proxy", "similar_images", "specific_site",
"print_urls", "print_size", "print_paths", "metadata", "extract_metadata", "socket_timeout",
"thumbnail", "thumbnail_only", "language", "prefix", "chromedriver", "browser", "related_images", "safe_search",
"thumbnail", "thumbnail_only", "language", "prefix", "browser", "related_images", "safe_search",
"no_numbering",
"offset", "no_download", "save_source", "silent_mode", "ignore_urls"]

Expand Down Expand Up @@ -146,9 +147,6 @@ def user_input():
help="A word that you would want to prefix in front of each image name", type=str,
required=False)
parser.add_argument('-px', '--proxy', help='specify a proxy address and port', type=str, required=False)
parser.add_argument('-cd', '--chromedriver',
help='specify the path to chromedriver executable in your local machine', type=str,
required=False)
parser.add_argument('-wb', '--browser',
help='Specify which driver to use', type=str,
required=False)
Expand Down Expand Up @@ -251,7 +249,7 @@ def download_page(self, url):
sys.exit()

# Download Page for more than 100 images
def download_extended_page(self, url, chromedriver, browser):
def download_extended_page(self, url, browser):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
if sys.version_info[0] < 3:
Expand All @@ -265,11 +263,9 @@ def download_extended_page(self, url, chromedriver, browser):
browser = webdriver.Firefox()
else:
try:
browser = webdriver.Chrome(chromedriver, chrome_options=options)
browser = webdriver.Chrome(options=options)
except Exception as e:
print("Looks like we cannot locate the path the 'chromedriver' (use the '--chromedriver' "
"argument to specify the path to the executable.) or google chrome browser is not "
"installed on your machine (exception: %s)" % e)
print("selenium error (exception: %s)" % e)
sys.exit()
browser.set_window_size(1024, 768)

Expand Down Expand Up @@ -307,21 +303,21 @@ def download_extended_page(self, url, chromedriver, browser):

# Bypass "Before you continue" if it appears
try:
browser.find_element_by_css_selector("[aria-label='Accept all']").click()
browser.find_element(By.CSS_SELECTOR, "[aria-label='Accept all']").click()
time.sleep(1)
except selenium.common.exceptions.NoSuchElementException:
pass

print("Getting you a lot of images. This may take a few moments...")

element = browser.find_element_by_tag_name("body")
element = browser.find_element(By.TAG_NAME, "body")
# Scroll down
for i in range(50):
element.send_keys(Keys.PAGE_DOWN)
time.sleep(0.3)

try:
browser.find_element_by_xpath('//input[@value="Show more results"]').click()
browser.find_element(By.XPATH, '//input[@value="Show more results"]').click()
for i in range(50):
element.send_keys(Keys.PAGE_DOWN)
time.sleep(0.3) # bot id protection
Expand Down Expand Up @@ -404,7 +400,7 @@ def format_object(self, object):
main = data[3]
info = data[9]
if info is None:
info = data[11]
info = data[25]
formatted_object = {}
try:
formatted_object['image_height'] = main[2]
Expand Down Expand Up @@ -1108,7 +1104,7 @@ def download_executor(self, arguments):
if limit < 101:
images, tabs = self.download_page(url) # download page
else:
images, tabs = self.download_extended_page(url, arguments['chromedriver'], arguments['browser'])
images, tabs = self.download_extended_page(url, arguments['browser'])

if not arguments["silent_mode"]:
if arguments['no_download']:
Expand Down Expand Up @@ -1139,7 +1135,7 @@ def download_executor(self, arguments):
if limit < 101:
images, _ = self.download_page(value) # download page
else:
images, _ = self.download_extended_page(value, arguments['chromedriver'], arguments['browser'])
images, _ = self.download_extended_page(value, arguments['browser'])
self.create_directories(main_directory, final_search_term, arguments['thumbnail'],
arguments['thumbnail_only'])
self._get_all_items(images, main_directory, search_term + " - " + key, limit, arguments)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
selenium
selenium>4.0.0