使用 python selenium webdriver 上传图片到 google 图片搜索

Using python selenium webdriver to upload an image to google image search

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import sys, os
from time import sleep

class searcher():
    """
    Google reverse image search bot
    Dependincies:
        - Selenium
        - Chrome Webdriver
    """

    def __init__(self, headless=False):
        os.chdir('../images_backend')
        self.image_dir = os.getcwd()
        print(self.image_dir)

        platform = ""
        end = ""

        if 'linux' in sys.platform:
            platform = 'linux'
        elif 'win' in sys.platform and 'dar' not in sys.platform:
            end = '.exe'
            platform = 'win'
        elif 'dar' in sys.platform:
            platform = 'mac'


        options = webdriver.ChromeOptions()
        if headless:
            options.add_argument('--window-size=1920,1080')
            options.add_argument('headless')
            options.add_argument('--start-maximized')
        self.driver = webdriver.Chrome('../webdriver/' + platform + '/chromedriver' + end, options=options)


    def __del__(self):
        self.driver.close()

    def __open_image_dialog(self):
        self.driver.get("https://www.google.com/imghp?hl=EN")
        cam_button = self.driver.find_elements_by_xpath("//div[@aria-label=\"Search by image\" and @role=\"button\"]")[0]
        cam_button.click()
        upload_image = self.driver.find_elements_by_xpath("//div[@class=\"qbtbha sl\"]")[0]
        upload_image.click()
        self.upload_dialog = self.driver.find_elements_by_xpath("//input[@id=\"qbfile\" and @name=\"encoded_image\"]")[0]


    def open_shopping_section(self):
        shop_button = self.driver.find_element_by_xpath(".//a[text()=\"Shopping\" and @class=\"q qs\"]")
        shop_button.click()


    def select_lo2hi(self):
        b1 = self.driver.find_element_by_xpath(".//span[@class=\"Yf5aUd\"]")
        b1.click()
        sleep(1)
        self.driver.find_element_by_xpath(".//g-menu-item[.//div[text()=\"PRICE – LOW TO HIGH\"]]").click()


    def text(self):
        return self.driver.text

    **def upload_image(self, path):**
        try:
            self.upload_dialog
        except:
            self.__open_image_dialog()
        self.upload_dialog.send_keys(self.image_dir + "/" + path)

    def find_products(self):
        q = []
        products = self.driver.find_elements_by_xpath('//div[@class=\"uMfazd\"]')
        for prod in products:
            for i in range(3):
                link = prod.find_elements_by_xpath("//a[@class=\"EI11Pd p7n7Ze\" and @data-what=\"1\"]")[i]
                q += [link.get_attribute('href')]
        return list(set(q))

s = searcher(headless=False)
s.upload_image('teddybear.jpg')


print('opening shopping section')

s.open_shopping_section()

sleep(3)

print('select lo2hi')

s.select_lo2hi()


print(s.find_products())

s.driver.save_screenshot('screened.png')

del s

这是我现在正在尝试调试的代码。它使用 selenium chrome webdriver 和 python 3.8.

获取图像并通过自动反向 google 图像搜索运行它。

它给我一个错误详细信息:

backend\search_api.py", line 68, in upload_image self.upload_dialog AttributeError: 'searcher' object has no attribute 'upload_dialog'

我对使用 python 和 selenium 比较陌生,所以任何指点或建议将不胜感激:)

谢谢

编辑:看起来你已经部分工作了。由于我的评分很低,我不能评论,所以我在代码解决方案下面提出问题。

这是一个通过 Google 搜索 Selenium 反向图像的工作示例:

from selenium import webdriver
import os
import time

# Using Chrome to access web
driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"))

try:
    # Open the website
    driver.get('https://images.google.com/')

    # Find cam button
    cam_button = driver.find_elements_by_xpath("//div[@aria-label=\"Search by image\" and @role=\"button\"]")[0]
    cam_button.click()

    # Find upload tab
    upload_tab = driver.find_elements_by_xpath("//*[contains(text(), 'Upload an image')]")[0]
    upload_tab.click()

    # Find image input
    upload_btn = driver.find_element_by_name('encoded_image')
    upload_btn.send_keys(os.getcwd()+"/image.png")

    time.sleep(10)

except Exception as e:
    print(e)

driver.quit()

另外请问,运行open_image_dialog之前运行宁upload_dialog为什么不这样呢?

def upload_image(self, path):
        self.__open_image_dialog()
        self.upload_dialog.send_keys(self.image_dir + "/" + path)