我用python和selenium编写了代码。它会点击show-more-results按钮一次。我已经使用了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)'
答案 0 :(得分: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
这是一个快速草案,你必须修改它,但应该让你更接近你想要做的事情。 特别是如果您为网络断开连接,您希望打破该异常。