Selenium + Python:for ..(循环)不能按预期工作

时间:2017-04-07 16:48:32

标签: python python-2.7 selenium selenium-webdriver

这是我的代码。我要做的是打印所有酒店名称 搜索条件“BERLIN,GERMANY”的页面 当我按下Pycharm中的“运行”按钮时,屏幕上没有任何内容。 任何人都可以弄清楚我的代码中的错误是什么吗?

from selenium import webdriver    
from selenium.webdriver.common.by import By    
from selenium.webdriver.support import expected_conditions as EC    
from selenium.webdriver.support.wait import WebDriverWait

my_list = []

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

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

driver.find_element_by_css_selector("input#ss").send_keys("BERLIN, GERMANY")

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

hotel_name = driver.find_elements_by_css_selector(".sr-hotel__name")

visible_next=driver.find_element_by_css_selector(".paging-next")

driver.maximize_window()

for link in hotel_name:

        scrap_hotel_name = link.text

        print (scrap_hotel_name)

        my_list.append(scrap_hotel_name)

        while True:

            click_icon = WebDriverWait(driver, 4).until(EC.visibility_of_element_located([By.LINK_TEXT, 'Next page']))

            click_icon.click()

enter image description here

1 个答案:

答案 0 :(得分:0)

您的脚本逻辑中存在几个问题:

  1. 您的hotel_name定义了循环。这意味着您只需在第一页上获取一次元素列表;
  2. 您的visible_next变量已定义,但您没有使用它;
  3. while循环中的for循环意味着一次又一次地单击“下一步”按钮而不执行剩余的for循环行(它们将仅在第一次执行时执行一次)页)
  4. 尝试使用以下代码从所有网页获取酒店名称:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait 
    
    driver = webdriver.Chrome()
    driver.get('http://booking.com')
    driver.find_element_by_css_selector("input#ss").send_keys("BERLIN, GERMANY")
    driver.find_element_by_css_selector("button.sb-searchbox__button").click()
    my_list = []
    
    while True:
        WebDriverWait(driver, 5).until(EC.invisibility_of_element_located([By.CSS_SELECTOR, 'div[class^="sr-usp-overlay"]']))
        my_list.extend([hotel.text for hotel in driver.find_elements_by_css_selector('.sr-hotel__name')])
        try:
            WebDriverWait(driver, 5).until(EC.element_to_be_clickable([By.CSS_SELECTOR, "*[data-page-next]"])).click()
        except:
            break
    
    [print(hotel) for hotel in my_list]
    

    Python 2.x中用

    替换最后一行
    for hotel in my_list:
        print(hotel)