硒继续提供最后的迭代数据,而不是新的

时间:2019-07-14 16:25:49

标签: python selenium selenium-webdriver beautifulsoup selenium-chromedriver

我正在尝试从amazon.com搜索框的自动完成功能中抓取建议,当浏览器打开并按搜索框中的键时,我的算法在视觉上可以正常工作,但是从硒得到的结果却卡在了最后一次迭代:

这是我的代码:

from selenium import webdriver
from bs4 import BeautifulSoup
import time
all_char = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

browser = webdriver.Chrome('chromedriver.exe')
url = "https://www.amazon.com/"
browser.get(url)
search_box = browser.find_element_by_xpath('//input[@id="twotabsearchtextbox"]')


while True:
    for key in all_char:
        search_box.send_keys(key)
        time.sleep(2)
        search_box_html = BeautifulSoup(browser.page_source, "html.parser")
        suggestions = search_box_html.select('div[data-alias="aps"]')
        suggestions_list = []
        for suggestion in suggestions:
            suggestions_list.append(suggestion)
            print(suggestion.text)
        print('Finished Here Results Suggestions','\n')

        if len(suggestions_list) > 9:
            break
        search_box.send_keys('\b')

到达该部分时:如果len(suggestion)> 9:错误

它很好地完成了第一次迭代,然后陷入困境,却给了我相同的结果,而迭代继续在浏览器中进行,就像这样:

aaaaabag
Finished Here Results Suggestions 

aaaaabag
Finished Here Results Suggestions 

aaaaabag
Finished Here Results Suggestions

有人知道如何解决此问题,或者是什么原因引起的? 谢谢

0 个答案:

没有答案