尝试在python阵列中添加来自booking.com的酒店名称,同时自动点击下一页

时间:2017-03-31 09:44:53

标签: python python-2.7 python-3.x selenium selenium-chromedriver

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait

mylist=[]
my_category=""

driver=webdriver.Chrome("C:\\Python27\\selenium\\webdriver\\chrome\\chromedriver.exe")

driver.get('http://booking.com')

driver.find_element_by_css_selector("#ss").send_keys("Rhodes, Greece")
WebDriverWait(driver, 5)

driver.find_element_by_css_selector("button.sb-searchbox__button").submit()

category = driver.find_elements_by_css_selector("span.sr-hotel__name")

wait = WebDriverWait(driver, 10)

while True:
    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, '.paging-next')))
    try:
        for link in category:
            my_category = link.text
            print (my_category)
            mylist.append(my_category)
            element=wait.until(EC.visibility_of_element_located((By.LINK_TEXT, 'Next page')))
            element.click()
    except TimeoutException:
            break

这是我收到的错误消息:

C:\Python27\python.exe C:/Users/ΧΡΗΣΤΟΣ/PycharmProjects/HotelScrap/HotelScrapPackage/HotelScrapPythonFileIndex.py
Traceback (most recent call last):


File "C:/Users/�������/PycharmProjects/HotelScrap/HotelScrapPackage/HotelScrapPythonFileIndex.py", line 28, in <module>
    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, '.paging-next')))
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 


Process finished with exit code 1

1 个答案:

答案 0 :(得分:0)

您应该通过CLASS_NAMECSS_SELECTOR区分搜索中的班级名称使用情况:

假设我们有<a class="paging-next">

  • 如果使用By.CLASS_NAME来定位元素,则应将其用作

    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'paging-next')))

请注意,在课名('paging-next')之前没有点

  • 如果使用By.CSS_SELECTOR来定位元素,则应将其用作

    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.paging-next')))

请注意,在课名('.paging-next')之前是一个点