selenium.common.exceptions.NoSuchElementException:消息:{“errorMessage”:“无法找到id为'search-facet-city'的元素”

时间:2017-10-27 01:49:15

标签: python selenium phantomjs

我正在尝试使用Python 3,Selenium和PhantomJS来搜索以下网站:

https://health.usnews.com/best-hospitals/search

我需要找到一个搜索字段并在其中输入文本,然后按Enter键生成搜索结果。下面是我想要找到的搜索字段对应的HTML:

<div class="search-field-view">

<div class="block-tight">
    <label class="" for="search-facet-city">
        <input id="search-facet-city" autocomplete="off" name="city" 
type="text" data-field-type="text" placeholder="City, State or ZIP" 
value="" />
    </label>
</div>

</div>

以下是我的Python 3代码,它尝试使用ID“search-facet-city”找到此搜索字段。

def scrape(self):
    url = 'https://health.usnews.com/best-hospitals/search'
    location = 'Massachusetts'

    # Instantiate the driver
    driver = webdriver.PhantomJS()
    driver.get(url)
    driver.maximize_window()
    driver.implicitly_wait(10)

    elem = driver.find_element_by_id("search-facet-city")
    elem.send_keys(self.location)

    driver.close()

将文本输入搜索字段后,我需要从页面中删除一些结果。但是,我一直收到NoSuchElementException错误;尽管它存在,但它无法找到搜索框元素。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我在Chrome上试过这个:

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


url = 'https://health.usnews.com/best-hospitals/search'
location = 'Massachusetts'
# Instantiate the driver
driver = webdriver.Chrome(executable_path=r'/pathTo/chromedriver')
#driver = webdriver.PhantomJS(executable_path=r'/pathTo/phantomjs')
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.save_screenshot('out.png');
elem=wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@class='search-field-view']")))
span = elem.find_element_by_xpath("//span[@class='twitter-typeahead']")
input=span.find_element_by_xpath("//input[@class='tt-input' and @name='city']");
input.send_keys(location)
driver.save_screenshot('out2.png');

它有效。

但如果我尝试使用phantomJS,请在driver.save_screenshot('out.png');中获取:

enter image description here

正如@JonhGordon在评论中所说,该网站进行了一些检查。如果您想使用phantomJS,可以尝试更改desired_capabilitiesservice_args