![](/img/trans.png)
[英]Python+selenium code works fine in Chrome, but not in Firefox
[英]Selenium…Program Works Fine for Firefox, but not Chrome
下面的代碼在Firefox上可以正常使用,但是當我設置browser = webdriver.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
使用chrome,一切正常,直到我需要提取文本為止。 我收到此錯誤:
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
我得出的結論是硒絕對不是完美的。
我懷疑這里的關鍵問題在於顯示結果時發生的動畫 。
您需要讓selenium
知道您要等待它, 然后再通過implicitly_wait()
搜索元素 :
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.results")))
# okay, selenium, slow down, please
browser.implicitly_wait(3)
h3s = browser.find_elements_by_css_selector('h3.one')
(為我工作)
如果是等待問題,我在流暢的等待方面有很好的經驗:
https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.