Warum öffnet sich das Kontextmenü für das 2. Bild nicht?Python

Python-Programme
Guest
 Warum öffnet sich das Kontextmenü für das 2. Bild nicht?

Post by Guest »

Warum öffnet sich das Kontextmenü nicht für das 2. Bild,
Betriebssystem importieren
Importzeit
import pyautogui # Zum Simulieren von Tastaturaktionen
import pyperclip # Für den Zugriff auf die Zwischenablage
import request # Zum Herunterladen des Bildesaus Selenimport webdriver
von selenium.webdriver.common.by import By
von selenium.webdriver.common.action_chains import ActionChains
von selenium.webdriver.chrome.service import Service
von selenium.webdriver.support.ui import WebDriverWait
von selenium.webdriver.support import previous_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import uuid # Zum Generieren eindeutiger Dateinamendef download_image(url, save_folder="downloaded_images"):

Code: Select all

"""Function to download the image from the given URL."""

try:

# Make a request to get the image content

response = requests.get(url, stream=True)

response.raise_for_status()  # Raise an error for bad status codes

# Ensure the save folder exists

os.makedirs(save_folder, exist_ok=True)

# Use the last part of the URL as the image name

image_name = url.split("/")[-1]

save_path = os.path.join(save_folder, image_name)

# Save the image content to a file

with open(save_path, 'wb') as file:

for chunk in response.iter_content(1024):

file.write(chunk)

print(f"Downloaded image saved as: {save_path}")

except Exception as e:

print(f"Error downloading image: {e}")
def select_and_process_image(driver, image, wait, save_folder="downloaded_images"):

Code: Select all

"""Selects the image, opens it, and downloads the high-quality version."""

try:

# Click the image to open the side panel

image.click()

print(f"Clicked on an image")

# Wait for the side panel to open and ensure that the high-res image is present

wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'sFlh5c')))  # Wait until the high-res image is present

# Short delay to ensure the image is fully loaded

time.sleep(5)  # Wait for 5 seconds to allow the image to load

# Find the high-quality image in the side panel

high_quality_image = driver.find_element(By.CLASS_NAME, 'sFlh5c')

# Simulate right-click to trigger the context menu on the high-quality image

actions = ActionChains(driver)

actions.context_click(high_quality_image).perform()  # Right-click on the image

# Wait for the context menu to appear

time.sleep(1)  # Small delay for the context menu to show up

# Navigate the menu to select the "Copy image address"  option (which is 10th in this case)

for _ in range(10):  # Press down 9 times to reach the "Copy image address"

pyautogui.press('down')

time.sleep(0.1)  # Small delay to allow proper navigation in the menu

pyautogui.press('enter')  # Press Enter to copy the URL to the clipboard

time.sleep(1)  # Allow time for the clipboard action

# Now get the copied URL from the clipboard

image_url = pyperclip.paste()  # Get the copied URL from the clipboard

if image_url:

print(f"Image URL: {image_url}")

# Download the image

download_image(image_url, save_folder)

else:

print("No valid URL found for this image.")

except Exception as e:

print(f"An error occurred while processing the image: {e}")
def automate_chrome_search(url, save_folder="downloaded_images"):

Code: Select all

# Ensure the save folder exists

os.makedirs(save_folder, exist_ok=True)

# Set up Chrome driver

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

try:

driver.get(url)

# Wait for images to load and find all images with class 'H8Rx8c'

wait = WebDriverWait(driver, 15)

images = wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'H8Rx8c')))  # Wait for all images

# Loop through all found images

for index, image in enumerate(images):

select_and_process_image(driver, image, wait, save_folder)

# Optionally, wait for a short time before processing the next image (if needed)

time.sleep(2)

# After processing, re-fetch the updated list of images for the next iteration

images = driver.find_elements(By.CLASS_NAME, 'H8Rx8c')

except Exception as e:

print(f"An error occurred while processing the search: {e}")

finally:

# Close the browser

driver.quit()
Beispielverwendung:
search_url = „https://www.google.com/search?as_st=y&a ... tbs=&udm=2“< /p>
automate_chrome_search(search_url)
Ich möchte, dass das Bildseitenfeld automatisch geöffnet wird. Warten Sie 15 Sekunden, bevor Sie mit der rechten Maustaste klicken. Nachdem der Bot mit der rechten Maustaste geklickt hat, sollte er nach unten scrollen und die Bildadresse kopieren Das Bild wird heruntergeladen, aber es kommt nur vor, dass sich das Kontextmenü für das erste Bild nicht für das zweite Bild öffnet

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post