我认为对于那些理解硒工具的人现在会笑,但也许你可以分享你的知识,因为现在也真的想笑。
我的代码是:
def getZooverLinks(country):
global countries
countries = country
zooverWeb = "http://www.zoover.nl/"
url = zooverWeb + country
driver = webdriver.Firefox()
driver.get(url)
button = driver.find_element_by_class_name('next')
links = []
for page in xrange(1,4):
WebDriverWait(driver, 60).until(lambda driver :driver.find_element_by_class_name('next'))
divList = driver.find_elements_by_class_name('blue2')
for div in divList:
hrefTag = div.find_element_by_css_selector('a').get_attribute('href')
print(hrefTag)
newLink = zooverWeb + hrefTag
links.append(newLink)
button.click()
driver.implicitly_wait(10)
time.sleep(60)
return links
所以我想遍历所有结果页面并始终从具有class =“blue2”的div获取链接,然后按照“next”-link进入下一个结果页面。 但总是我得到一个StaleElementReferenceException说: “消息:在缓存中找不到元素 - 可能自查找以来页面已更改”
但页面的布局总是一样的。那么这里的问题是什么?点击后的网址是否因为页面更改而没有移交给驱动程序?我怎么能这样做?
答案 0 :(得分:1)
跟踪这个特定网站的分页有点棘手。
以下是帮助我克服StaleElementReferenceException
:
工作代码:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
country = "albanie"
zooverWeb = "http://www.zoover.nl/"
url = zooverWeb + country
driver = webdriver.Firefox()
driver.get(url)
driver.implicitly_wait(10)
links = []
for page in xrange(1, 4):
# tricky part - waiting for the page number on the top to appear
if page > 1:
WebDriverWait(driver, 60).until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, 'div.entityPagingTop strong'), str(page)))
else:
WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CLASS_NAME, 'next')))
divList = driver.find_elements_by_class_name('blue2')
for div in divList:
hrefTag = div.find_element_by_css_selector('a').get_attribute('href')
newLink = zooverWeb + hrefTag
links.append(newLink)
driver.find_element_by_class_name("next").click()
print links