Javascript 使用selenium和Python在iframe中显示图像的src

Javascript 使用selenium和Python在iframe中显示图像的src,javascript,python,selenium,iframe,Javascript,Python,Selenium,Iframe,我正在尝试使用Python和selenium从shapeNet自动下载图像。我就快到了,但最后一步却躲开了我 from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By

我正在尝试使用Python和selenium从shapeNet自动下载图像。我就快到了,但最后一步却躲开了我

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.socks", "yourproxy")
profile.set_preference("network.proxy.socks_port", number_of_port)
#browser = webdriver.Firefox(firefox_profile=profile)
browser = webdriver.Firefox()

browser.get('https://www.shapenet.org/taxonomy-viewer')
#Page is long to load
wait = WebDriverWait(browser, 30)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='02958343_anchor']")))
linkElem = browser.find_element_by_xpath("//*[@id='02958343_anchor']")
linkElem.click()
#Page is also long to display iframe
element = wait.until(EC.element_to_be_clickable((By.ID, "model_3dw_bcf0b18a19bce6d91ad107790a9e2d51")))
linkElem = browser.find_element_by_id("model_3dw_bcf0b18a19bce6d91ad107790a9e2d51")
linkElem.click()
#iframe slow to be displayed
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'viewerIframe')))
到目前为止,一切都进展顺利,我们进入了iframe。下一行行行得通,但我必须使用time.sleep来让它工作它有点难看,但我不知道有什么其他选择,这不是我问题的核心:

import time
#does not work have to use time.sleep
#element = wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/div[3]/h4")))
time.sleep(20)
linkElem = browser.find_element_by_xpath("/html/body/div[3]/div[3]/h4")
linkElem.click()
现在,我想下载刚才在单击打开的折叠菜单中显示的一个图像,以便使用开发工具找到其xpath:

img = browser.find_element_by_xpath("/html/body/div[3]/div[3]/div/div/div/span/img")
src = img.get_attribute('src')
现在它可以访问img,但src是没有的,直到我手动点击网页。为什么呢?我做错了什么

PS:最后一步是:

os.system("wget %s --no-check-certificate"%src)
您可以使用以下xpath代替xpath/html/body/div[3]/div[3]/div/div/span/img:


谢谢你的回答,我投了赞成票!你愿意详细说明你的答案吗?比如它是如何工作的,而另一个没有?检查html,它看起来像类是imageResult而不是searchResult否?我看不出您是如何找到要访问的图像的id的?加上班级扩大意味着什么?是放大图片吗?这么多问题!首先,我必须承认,您的详细代码帮助我弄清了您的问题,但是您可以更详细地指定手动步骤。第二,虽然是绝对的xpath,但您仍然幸运地通过了xpath/html/body/div[3]/div[3]/h4。这就是你放松控制的地方。我不想重复绝对的xpath方式,所以我做了一个//div,并以类作为searchResult潜入我的业务领域,抓取兄弟标记以提取src属性。我不知道班级扩大意味着什么,但它就在那里。谢谢你的评论!我将尝试修改我的问题,使其更精确。我会接受你的答案,同时我会在下周再做一次,以确保我正确理解了所有步骤!
img = browser.find_element_by_xpath("/html/body/div[3]/div[3]//div[@class='searchResult' and @id='image.3dw.bcf0b18a19bce6d91ad107790a9e2d51.0']/img[@class='enlarge']")
src = img.get_attribute('src')