1
我正在使用Selenium,Python和PhantomJS编写爬网程序来使用Google的反向图像搜索。到目前为止,我已经成功地上传图片并在第一页上抓取搜索结果。但是,当我尝试点击搜索结果导航时,我收到一个StaleElementReferenceError。我在很多文章中已经阅读过它,但仍然无法实施解决方案。这里是破解的代码:StaleElementReferenceException selenium webdriver python
ele7 = browser.find_element_by_id("nav")
ele5 = ele7.find_elements_by_class_name("fl")
count = 0
for elem in ele5:
if count <= 2:
print str(elem.get_attribute("href"))
elem.click()
browser.implicitly_wait(20)
ele6 = browser.find_elements_by_class_name("rc")
for result in ele6:
f = result.find_elements_by_class_name("r")
for line in f:
link = line.find_elements_by_tag_name("a")[0].get_attribute("href")
links.append(link)
parsed_uri = urlparse(link)
domains.append('{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri))
count += 1
代码在打印时打破str(elem.get_attribute("href"))
。我该如何解决这个问题?
在此先感谢。