为什么 wd.switch_to.default_content() 不返回原始网页?

Why is wd.switch_to.default_content() not returning to original webpage?

我正在为 bing 制作图像抓取工具,因为它具有完善的按许可搜索功能。 无论如何,我似乎无法获得多个图像,因为在切换到 iframe 以从第一张图像获取 src 后我无法返回主页。有什么建议吗?

    import selenium
    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    
    image_url = []
    
    wd = webdriver.Firefox()
    
    wait = WebDriverWait(wd, 10)

    ##search bing for images
    search_url = f"https://www.bing.com/images/search?q=cat%20clipart&qs=n&form=QBIR&qft=%20filterui%3Alicense-L1%20filterui%3Aimagesize-large&sp=-1&pq=good%20clipart&sc=8-12&cvid=493F746CDC7B4E70BF3BEDDA3CF674E1&first=1&scenario=ImageBasicHover"
        # load the page
    wd.get(search_url)
    
    thumbnail_result = wd.find_element_by_css_selector("img.mimg")
    
    thumbnail_result.click()
    
    wait.until(
            EC.frame_to_be_available_and_switch_to_it((By.ID, "OverlayIFrame"))
        )
    wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "img.nofocus"))
        )
    
    actual_image = wd.find_element_by_css_selector('img.nofocus')
    
    image_url.append(actual_image.get_attribute('src'))

    ##This is where it doesn't switch back to the original webpage    
    wd.switch_to.default_content()

这是包含您建议的更改的代码:

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

image_url = []

wd = webdriver.Firefox()

wait = WebDriverWait(wd, 10)

search_url = f"https://www.bing.com/images/search?q=cat%20clipart&qs=n&form=QBIR&qft=%20filterui%3Alicense-L1%20filterui%3Aimagesize-large&sp=-1&pq=good%20clipart&sc=8-12&cvid=493F746CDC7B4E70BF3BEDDA3CF674E1&first=1&scenario=ImageBasicHover"
    # load the page
wd.get(search_url)

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img.mimg"))).click()

wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "OverlayIFrame"))
    )
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "img.nofocus")))

actual_image = wd.find_element_by_css_selector('img.nofocus')

image_url.append(actual_image.get_attribute('src'))

print(image_url)

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#close"))).click()

wd.switch_to.default_content()

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img.mimg"))).click()

wd.switch_to.default_content()不是return你回到主页面。它是用来从iframe出来的。你需要点击close按钮来关闭图像页。一旦你点击它,你就会看到主页

image_url.append(actual_image.get_attribute('src'))

#Click on close button to go back to main page
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#close"))).click()

##Now jump out from iframe
wd.switch_to.default_content()