This is a link of Google image search
例如,我想得到它的第一个结果。
我使用下面的代码点击第一张图片,它可以正常工作。
driver.find_elements_by_xpath("//*[@id='rg_s']/div[1]/a/img")[0].click()
我想下载原始图片。所以我用
img_urllist = driver.find_elements_by_xpath("//*[@id='irc_cc']/div[2]/div[1]/div[2]/div[2]/a/img")
但我在这里得到了一个 NoneType 的东西。我不知道为什么。 xpath 是正确的。为什么我得到一个NoneType。
selenium.common.exceptions.ElementNotInteractableException:消息:
from selenium import webdriver
from binascii import a2b_base64
from selenium.webdriver.common.keys import Keys
import os
import json
import urllib2
import sys
import time
import re
# adding path to geckodriver to the OS environment variable
os.environ["PATH"] += os.pathsep + os.getcwd()
download_path = "dataset/"
def main():
searchtext = "wallpaper"
num_requested = 10
if not os.path.exists(download_path + searchtext.replace(" ", "_")):
os.makedirs(download_path + searchtext.replace(" ", "_"))
url = "https://www.google.co.in/search?q="+searchtext+"&source=lnms&tbm=isch"
driver = webdriver.Firefox()
driver.get(url)
headers = {}
headers['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
extensions = [ "jpg", "jpeg", "png", "gif" ]
img_count = 0
downloaded_img_count = 0
driver.find_elements_by_xpath("//*[@id='rg_s']/div[1]/a/img")[0].click()
img_urllist = driver.find_elements_by_xpath("//*[@id='irc_cc']/div[2]/div[1]/div[2]/div[2]/a/img")
print img_urllist
img_urllist[0].click()
print "Total downloaded: ", downloaded_img_count, "/", img_count
driver.quit()
if __name__ == "__main__":
main()
答案 0 :(得分:0)
尝试使用xpath以下。我尝试检查元素,发现xpath与你的不同
img_urllist = driver.find_elements_by_xpath("//*[@id='irc_cc']/div[2]/div[1]/div[2]/div[3]/a/img")