WebDriverWait(selenium)无法工作
我写了一段用Python和Selenium的代码。它只点击了一次“显示更多结果”的按钮。我使用了WebDriverWait来让页面加载,但还是没有用。
for j in range(100):
driver.find_element_by_xpath('.//div[@id="show-more-results"]').click()
WebDriverWait(driver, 50).until(lambda driver : driver.find_element_by_xpath('.//div[@id="show-more-results"]'))
出现了以下错误
Traceback (most recent call last):
File "python23.py", line 20, in <module>
driver.find_element_by_xpath('.//div[@id="show-more-results"]').click()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 60, in click
self._execute(Command.CLICK_ELEMENT)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 370, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 166, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: u'element not visible\n (Session info: chrome=34.0.1847.132)\n (Driver info: chromedriver=2.9.248304,platform=Linux 3.13.0-24-generic x86_64)'
1 个回答
0
接着上一个回答,我对脚本做了一些修改。
你需要自己找一个条件来结束循环,我建议找一个只出现在最后一页的元素,当你到达这个元素时就结束循环。
import time
driver = webdriver.Firefox()
driver.get("http://www.flipkart.com/mobiles/pr? p%5B%5D=sort%3Dfeatured&sid=tyy%2C4io&ref=659eb948-c365-492c-99ef-59bd9f0427c6")
time.sleep(3)
driver.maximize_window() # maximize window to see elements
for i in range(4):
print "Scrolling"
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # scroll to bottom of page
time.sleep(4)
while True: #
try:
print "Scrolling to bottom of page" # need to sroll to make show-more-results visible
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # scroll to bottom of page
time.sleep(2)# wait until you get to bottom of page to avoid "element not visible"
print "Clicking show more results"
driver.find_element_by_xpath('//*[@id="show-more-results"]').click()
# click load more button, needs to be done until you reach the end.
time.sleep(10) # adjust sleep to suit your connection
except Exception as e:
print "This happened ",e
print "Trying again"
continue
elem=[]
elem=driver.find_elements_by_xpath('.//div[@class="pu-title fk-font-13"]')
for e in elem:
print e.text
这只是一个快速的草稿,你可能需要对它进行一些修改,但应该能让你更接近你想要实现的目标。
特别是如果你网络断开了,你需要在这个异常情况下结束循环。