[英]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
Here is the code that I am trying to debug right now.这是我现在要调试的代码。 It takes an image and runs it through a automated reverse google image search using selenium chrome webdriver with python 3.8.
它获取图像并使用 selenium chrome webdriver 和 python 3.8 通过自动反向谷歌图像搜索运行它。
It gives me an error detailing:它给了我一个错误详细信息:
backend\search_api.py", line 68, in upload_image self.upload_dialog AttributeError: 'searcher' object has no attribute 'upload_dialog'
I am relatively new to using python with selenium, so any pointers or suggestions would be greatly appreciated:)我对使用 python 和 selenium 比较陌生,因此非常感谢任何指点或建议:)
Thanks谢谢
EDIT : It seems like you already have it partially working.编辑:看起来你已经部分工作了。 I can't comment since my rating is low, so I pose a question below the code solution.
我无法发表评论,因为我的评分很低,所以我在代码解决方案下方提出了一个问题。
Here's a working example of Selenium reverse image searching through 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()
Also, may I ask, why wouldn't you run open_image_dialog
before running upload_dialog
like so?另外,请问,您为什么不像这样在运行
upload_dialog
之前先运行open_image_dialog
?
def upload_image(self, path):
self.__open_image_dialog()
self.upload_dialog.send_keys(self.image_dir + "/" + path)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.