尝试通过实践来学习python。
results = driver.find_element_by_class_name("n_p").text
results = int(re.sub(r'[^\d,\.]','',results))
expected_pages = int(results / 20)
print(results)
print(expected_pages)
count = 1
while count < expected_pages:
time.sleep(10)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
wait = WebDriverWait(driver, 10)
time.sleep(5)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), ' Show More Results')]")))
actions = ActionChains(driver)
actions.move_to_element(element).perform()
actions.click().perform()
count = count + 1
print(count)
print("All Done!")
我执行时收到的输出:
3670 #results
183 #expected_pages
2 #count
然后它给了我以下错误:
追踪(最近一次呼叫最后一次):
文件&#34; C:\ Python34 \ TryScroll.py&#34;,第35行,
element = wait.until(EC.element_to_be_clickable((By.XPATH,&#34; // * [包含(text(),&#39;显示更多结果&#39;)]&#34;))) 文件&#34; C:\ Python34 \ lib \ site-packages \ selenium \ webdriver \ support \ wait.py&#34;,line 80,直到
引发TimeoutException(消息,屏幕,堆栈跟踪)
selenium.common.exceptions.TimeoutException:消息:
我可以看到有问题的元素,即它已滚动到元素所在页面的末尾,尝试添加几秒钟,看看是否会修复它没有工作,检查元素:
<div id="scroll3" class="fm2 p8 cur m_bt2" onclick="javascript:displayResultsLogin('scroll3')"> Show More Results </div>
Xpath: //*[@id="scroll3"]