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

时间:2018-01-19 16:39:32

标签: 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


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是None,直到我在网页上手动点击。这是为什么 ?我做错了什么?

PS:最后一步是:

os.system("wget %s --no-check-certificate"%src)

1 个答案:

答案 0 :(得分:1)

您可以使用以下xpath("/html/body/div[3]/div[3]/div/div/div/span/img")

代替xpath
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')