简体   繁体   English

Selenium + Python:for ..(循环)无法按预期工作

[英]Selenium+Python: for.. (loop) doesn't work as expected

Here is my code. 这是我的代码。 What I am trying to do is to print all hotel names in all pages with the search term "BERLIN, GERMANY". 我想要做的是在所有页面上用搜索词“ BERLIN,GERMANY”打印所有酒店名称。 While I press the 'run' button in Pycharm there is nothing printed in screen. 当我按Pycharm中的“运行”按钮时,屏幕上没有打印任何内容。 Can anyone figure out what is the mistake in my code? 任何人都可以找出我的代码中的错误吗?

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()

在此处输入图片说明

There are couple of issues in the logic of your script: 脚本逻辑中有几个问题:

  1. Your hotel_name defined out of loop. 您的hotel_name定义为循环外。 That means that you gets the list of elements just once- on the first page only; 这意味着您只能在首页上一次获得元素列表。
  2. Your visible_next variable defined, but you don't use it; 您定义了visible_next变量,但是您不使用它;
  3. The while loop inside for loop intents to click "Next" button time after time without executing the rest of for loop lines (they will be executed only once- on the first page) while内部循环for循环意图单击“下一步”按钮,一次又一次不执行其余for循环线(他们只会once-在第一页上执行)

Try below code to get hotel names from all of the pages: 尝试以下代码从所有页面获取酒店名称:

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]

In Python 2.x replace last line with Python 2.x将最后一行替换为

for hotel in my_list:
    print(hotel)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM