下面的代碼適用於Firefox,但是當我設置browser = webdriver.Chrome()時,我遇到了一些問題。Selenium ... Program Works適用於Firefox,但不適用於Chrome
browser = webdriver.Firefox()
browser.get('https://www.voilanorbert.com/')
inputElement = browser.find_element_by_id("form-search-name")
inputElement.send_keys(leadslist[i][0])
inputElement = browser.find_element_by_id("form-search-domain")
inputElement.send_keys(leadslist[i][1])
searchbutton = browser.find_element_by_name("search")
searchbutton.click()
wait = WebDriverWait(browser, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.results")))
h3s = browser.find_elements_by_css_selector('h3.one')
h3 = next(element for element in h3s if element.is_displayed())
result = h3.text
隨着鉻,一切都很好,直到我需要提取文本。我得到這個錯誤:
h3 = next(element for element in h3s if element.is_displayed())
StopIteration
編輯
問題再次顯示出來兩個Firefox和Chrome。我甚至加了一口流利的等待。
wait = WebDriverWait(browser, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.results")))
wait2 = WebDriverWait(browser, 3000, poll_frequency=100, ignored_exceptions=[ElementNotVisibleException])
wait2.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "h3.one")))
h3s = browser.find_elements_by_css_selector('h3.one')
h3 = next(element for element in h3s if element.is_displayed())
result = h3.text
我得出結論,硒是絕對不完美的。
如何定義'h3s'? – alecxe 2015-03-30 19:39:25
編輯顯示。只是忘了張貼該行。 – ohbrobig 2015-03-30 20:41:30