【问题标题】:Selenium Python : NoSuchElementException硒 Python:NoSuchElementException
【发布时间】:2017-07-25 09:37:59
【问题描述】:

我正在尝试从 Bing 中抓取图像。我正在使用 Selenium 并尝试提取图像的源链接。

    driver = webdriver.Firefox()
    driver.get("http://www.bing.com/images")
    elem = driver.find_element_by_id("sb_form_q")
    elem.clear()
    elem.send_keys("wheat zinc deficiency")
    elem.send_keys(Keys.RETURN)
    time.sleep(10)
    driver.find_element_by_class_name("mimg").click()
    driver.implicitly_wait(10)
    driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[1]/div/span[1]/div/div/div/div[1]/span/span/img").click()

最后一行显示错误,即

selenium.common.exceptions.NoSuchElementException: 消息:无法定位元素

我试图通过等待页面加载来避免竞争状况。 我使用firefox的firebug插件获得了xpath。

【问题讨论】:

    标签: python selenium xpath selenium-firefoxdriver


    【解决方案1】:

    图像在iframe 中打开,因此要处理它,您需要先切换到iframe

    driver.find_element_by_class_name("mimg").click()
    driver.switch_to_frame('OverlayIFrame')
    image = driver.find_element_by_xpath('//img[@class="mainImage accessible nofocus"]')
    print(image.get_attribute('src'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-30
      • 2021-01-04
      • 2020-08-23
      • 2020-09-13
      • 2014-07-11
      • 2017-12-30
      • 2016-04-17
      • 2019-04-09
      相关资源
      最近更新 更多