使用Firefox或Chrome Web驱动程序无济于事。
使用不同的等待条件组合也无济于事。below,我要运行的代码的MWE:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options as OptionsFirefox
from selenium.webdriver.chrome.options import Options as OptionsChrome
from PIL import Image
from io import BytesIO
test_url = "https://cdn-creatives.adikteev.com/Creatives/demoLink/MLEngine/index.html?MRAID_320X480_AWEM_CradleEmpires_Aug20/creative-e03f09e5.min.js"
id_iframe = "mainIframe"
# Setting up the driver.
# options = OptionsFirefox()
# options.headless = True
# driver = webdriver.Firefox(options=options)
options = OptionsChrome()
options.headless = True
driver = webdriver.Chrome(options=options)
# Getting the url.
driver.get(test_url)
WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.ID, id_iframe)))
# Getiing the iframe with its informations:
elem = driver.find_element(By.ID, id_iframe)
#
location = elem.location
size = elem.size
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
ic(elem.location)
ic(elem.size)
# Waits (might help ?).
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it(id_iframe))
# Saving screenshots:
# Complete screenshot.
img_png = driver.get_screenshot_as_png()
driver.save_screenshot("full_screen_headless_on.png")
img_crop = Image.open(BytesIO(img_png))
img_crop = img_crop.crop((left, top, right, bottom)) # defines crop points
# Screenshot cropped to the Iframe.
img_crop.save( "iframe_screen_headless_on.png" ) # saves new cropped image
driver.quit()
如果有人有解决方案,这将不胜感激:-)!
selenuium我也有同样的问题。
在解决URL后,我的案件额外等待,例如:
...
driver.get(url)
time.sleep(10)
WebDriverWait(driver, 20).until(
EC.frame_to_be_available_and_switch_to_it((By.ID, id_iframe))
)
...
我实际上无法解释为什么这样做,我没有深入文档,但它有所帮助。