Home>

I would like to access the link below to get a map image.

NoSuchElementException Traceback (most recent call last)
<ipython-input-53-641d17a2d654>in<module>
     25 time.sleep (5)
--->26 element = driver.find_element_by_css_selector ("# thunder-image-big")
     27
     28 print (element)
C: \ ProgramData \ Anaconda3 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py in find_element_by_css_selector (self, css_selector)
    596 element = driver.find_element_by_css_selector ('# foo')
    597 "" "
->598 return self.find_element (by = By.CSS_SELECTOR, value = css_selector)
    599
    600 def find_elements_by_css_selector (self, css_selector):
C: \ ProgramData \ Anaconda3 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py in find_element (self, by, value)
    974 by = By.CSS_SELECTOR
    975 value ='[name = "% s"]'% value
->976 return self.execute (Command.FIND_ELEMENT, {
    977'using': by,
    978'value': value}) ['value']
C: \ ProgramData \ Anaconda3 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py in execute (self, driver_command, params)
    319 response = self.command_executor.execute (driver_command, params)
    320 if response:
->321 self.error_handler.check_response (response)
    322 response ['value'] = self._unwrap_value (
    323 response.get ('value', None))
C: \ ProgramData \ Anaconda3 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py in check_response (self, response)
    240 alert_text = value ['alert'] .get ('text')
    241 raise exception_class (message, screen, stacktrace, alert_text)
->242 raise exception_class (message, screen, stacktrace)
    243
    244 def _value_or_default (self, obj, key, default):
NoSuchElementException: Message: Unable to find element with css selector == # thunder-image-big
Corresponding source code
import time
from selenium import webdriver
Start #IE
# Describe the driver storage directory
driver = webdriver.Ie ("C: \\ Users \\ oooo \\ Desktop \\ Python \\ IEDriverServer.exe")
#Access the power grid
time.sleep (5)
element = driver.find_element_by_css_selector ("# thunder-image-big")
print (element)
What I tried

The element could not be acquired even if the class or id was specified.
I checked with soup.find_all ("class"), but I couldn't get the class of the image I wanted to get.

Supplementary information (FW/tool version, etc.)

Browser: IE11

  • Answer # 1

    Map imageiframeIt seems that it is.

    driver.get ('target page URL')
    iframe = driver.find_element_by_id ('target frame ID')
    driver.switch_to.frame (iframe)

    Image download example

    import time
    from selenium import webdriver
    import chromedriver_binary
    import urllib.request
    driver = webdriver.Chrome ()
    driver.get ('target page URL')
    iframe = driver.find_element_by_id ('target frame ID')
    driver.switch_to.frame (iframe)
    time.sleep (5)
    element = driver.find_element_by_css_selector ("target image ID"). get_attribute ("src")
    urllib.request.urlretrieve (element,'target.png')

    Target ???Please replace as appropriate.