【问题标题】:Web Scraping error in while using Search.submit()使用 Search.submit() 时出现 Web Scraping 错误
【发布时间】:2021-04-08 21:11:52
【问题描述】:

我正在尝试对来自以下位置的天气数据进行网络抓取https://www.wunderground.com/history/daily/us/dc/washington/KDCA

这里是 HTML:

<div _ngcontent-app-root-c136="" id="wuForm" class="ui-front wu-form">
   <div _ngcontent-app-root-c136="" id="wuSearch-contain" class="wu-search-contain ng-star-inserted"><label _ngcontent-app-root-c136="" for="wuSearch" class="visuallyHidden">Search</label><input _ngcontent-app-root-c136="" type="search" name="query" value="" id="wuSearch" placeholder="Search Locations" aria-label="Search" autocomplete="off" class="wu-search ng-untouched ng-pristine ng-valid"><span _ngcontent-app-root-c136="" class="close-search"><i _ngcontent-app-root-c136="" class="material-icons">close</i></span><span _ngcontent-app-root-c136="" class="geolocate-wrap"><i _ngcontent-app-root-c136="" class="material-icons">gps_fixed</i></span></div>
   <!----><!---->
   <search-autocomplete _ngcontent-app-root-c136="" _nghost-app-root-c135="">
      <ul _ngcontent-app-root-c135="" tabindex="0" class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all hide">
         <li _ngcontent-app-root-c135="" class="ui-autocomplete-geolocate ng-star-inserted">
            <div _ngcontent-app-root-c135="" class="mimic-a menu-geolocate"><i _ngcontent-app-root-c135="" class="material-icons">gps_fixed</i>Find Nearest Station </div>
         </li>
         <!----><!----><!----><!----><!----><!---->
         <li _ngcontent-app-root-c135="" class="ui-autocomplete-last ui-menu-item manage-favorites"><a _ngcontent-app-root-c135="" tabindex="-1" href="/member/favorites" class="ui-corner-all">Manage Favorite Cities</a></li>
      </ul>
   </search-autocomplete>
</div>

我开始了

driver = webdriver.Chrome(r'C:\Users\bno00\Downloads\chromedriver_win32\chromedriver.exe')
driver.get('https://www.wunderground.com/history/daily/us/dc/washington/KDCA')

它工作正常 然后当我尝试使用 .submit() 将邮政编码发送到搜索字段时:

zipcode='22202'
xpath='//*[@id="wuSearch"]'
search=driver.find_element_by_xpath(xpath)
search.clear() 
search.send_keys(zipcode)
search.submit() #error occurs here

错误是:

NoSuchElementException:消息:没有这样的元素:无法定位 元素:{"method":"xpath","selector":"./ancestor-or-self::form"} (会话信息:chrome=89.0.4389.114)

【问题讨论】:

    标签: python selenium web-scraping data-science data-analysis


    【解决方案1】:

    &lt;input&gt; 标签不是表单的一部分。你不能提交。您要么需要发送换行符,要么点击触发搜索的gps_fixed 图标。

    【讨论】:

      【解决方案2】:

      尝试使用以下方法:

      input_field = driver.find_element_by_css_selector("input[id=wuSearch]")
      input_field.click()
      input_field.send_keys("some zip code")
      

      或者只是:

      input_field = driver.find_element_by_id("wuSearch")
      input_field.send_keys("some zip code")
      

      尽量避免click()。我不确定它是否真的需要。 输入邮政编码后,您应该等待搜索结果出现。

      【讨论】:

        【解决方案3】:
        wait = WebDriverWait(driver, 10)
        driver.get('https://www.wunderground.com/history/daily/us/dc/washington/KDCA')
        zipcode='22202'
        xpath='//*[@id="wuSearch"]'    
        search=wait.until(EC.element_to_be_clickable((By.XPATH,xpath)))
        search.clear() 
        search.send_keys(zipcode)
        time.sleep(1)
        search.send_keys(Keys.ENTER)
        

        稍微延迟一下。使用 webdriver wait 确保您将邮政编码发送到输入,然后发送 Keys.Enter。

        导入

        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.common.keys import Keys
        from time import sleep
        

        【讨论】:

          猜你喜欢
          • 2021-10-10
          • 1970-01-01
          • 1970-01-01
          • 2020-12-18
          • 2022-01-08
          • 1970-01-01
          • 1970-01-01
          • 2021-07-13
          • 2018-02-25
          相关资源
          最近更新 更多