gpt4 book ai didi

javascript - 使用selenium和Python在iframe内显示图像的src

转载 作者:行者123 更新时间:2023-12-03 02:40:26 25 4
gpt4 key购买 nike

我正在尝试使用 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)

最佳答案

您可以使用以下 ,而不是 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')

关于javascript - 使用selenium和Python在iframe内显示图像的src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48345699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com