索引错误:迭代两个 webelement 列表时列表索引超出范围

我正在尝试在终端上打印结果,但收到以下错误消息:


IndexError: list index out of range

以下是代码,提前感谢您的帮助。这个领域的真正初学者。


import pandas as pd

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

import time


option = Options()

option.add_argument("--disable-infobars")

option.add_argument("start-maximized")

option.add_argument("--disable-extensions")

option.add_experimental_option("excludeSwitches", ['enable-automation'])


# Pass the argument 1 to allow and 2 to block

option.add_experimental_option("prefs", { 

    "profile.default_content_setting_values.notifications": 1 

})

driver = webdriver.Chrome(chrome_options=option, executable_path='C:\\Users\\Sheik\\Desktop\\web crawling\\chromedriver.exe')


driver.implicitly_wait(5000)


url = "https://www.yell.com/"


driver.get(url)


search_query_path = driver.find_element_by_xpath('''//*[@id="search_keyword"]''')

search_query_path.click()

search_query_path.send_keys("Garage Services")

search_city_path = driver.find_element_by_xpath('''//*[@id="search_location"]''')

search_city_path.click()

search_city_path.send_keys("London")

search_btn = driver.find_element_by_xpath('''//*[@id="searchBoxForm"]/fieldset/div[1]/div[3]/button''')

search_btn.click()



names = driver.find_elements_by_class_name("businessCapsule--name")

address = driver.find_elements_by_class_name("businessCapsule--address")


num_page_items = len(names)

for i in range(num_page_items):

    print(f"{names[num_page_items].text} : {address[num_page_items].text}")


driver.close()


收到一只叮咚
浏览 74回答 3
3回答

慕田峪9158850

您希望使用索引来循环访问 和 ,而不是列表大小inamesaddressfor i in range(num_page_items):     print(f"{names[i].text} : {address[i].text}")或者只是循环使用两者zipfor name, ad in zip(names, address):     print(f"{name.text} : {ad.text}")

米脂

您正在尝试访问具有其长度的列表,而不是迭代器变量iprint(f"{names[num_page_items].text} : {address[num_page_items].text}")自print(f"{names[i].text} : {address[i].text}")

守着星空守着你

你很接近。要从网页 https://www.yell.com/ 中提取所有名称和地址,您必须诱导WebDriverWait,并且可以使用以下任一定位器策略:visibility_of_all_elements_located()代码块:names = [my_elem.text for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "span.businessCapsule--name")))]address = [my_elem.text for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.businessCapsule--address")))]for i,j in zip(names, address):    print("{} address is {}".format(i, j))注意:您必须添加以下导入:from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as EC控制台输出:Diagnostic Services And Repairs Ltd address is 7.7 mi | R/O 310 Green Lanes, London, N13 5TTMaypole Motors Ltd address is 11.5 mi | We serve London | Surbiton, KT6Allen & Hall Motor Engineers Ltd address is 13.3 mi | We serve London | Romford, RM7Ryecraft Motors & Body Shop address is 4.1 mi | 1a Old James St, London, SE15 3TSM & K Garage Services address is 7.5 mi | 4 Oak Grove Rd, London, SE20 7RQDaytona Garage address is 4.9 mi | 98 Vale Rd, London, N4 1PZMobile Hybrid Repair Ltd address is 37.8 mi | We serve London | Reading, RG30C & E Motors address is 5.1 mi | Unit 6 Brookmarsh Trading Estate, Norman Rd, London, SE10 9QEStevens Motors address is 8.2 mi | 3a Wellington Rd, London, E11 2ANM V Motor Repairs address is 5.6 mi | 7 Bidder St, London, E16 4STCarpenters Garage address is 6.1 mi | 69 Bickersteth Rd, London, SW17 9SHCar Care Ealing address is 8.2 mi | 199 Northfield Avenue, London, W13 9QUGerman Car Centre address is 7.3 mi | Unit 3, Hyde Estate Road, LONDON, NW9 6JXDefoe Tyres Ltd address is 4.2 mi | 1a Defoe Rd, London, N16 0EPKwik Kar Service Centre address is 3.8 mi | 11 West Hampstead Mews, London, NW6 3BBDPF Specialist Clinic address is 5.7 mi | 115 Lea Bridge Rd, London, E10 7AGBlueflash Garage address is 3 mi | 21-25 Bedford Rd, Clapham North, London, SW4 7SHNational Tyres and Autocare address is 7.1 mi | 57 Kingston Road, London, SW19 1JNNational Tyres and Autocare address is 7.8 mi | 92-96 St Marys Road, Ealing, London, W5 5EXMerton Autotechnics address is 7 mi | Unit 5 Station Rd, London, SW19 2LPThe Old Forge Garage address is 11.9 mi | We serve London | Belvedere, DA17Green Man Tyre & Exhaust Ltd address is 8.8 mi | 1308 High Rd, London, N20 9HJCastle Motors Ltd address is 6.9 mi | The Rear Of Number 1 Sansom Rd, London, E11 3EYNational Tyres and Autocare address is 7.3 mi | We serve London | The Hyde, Hendon, NW9Walthamstow Village Garage address is 7.3 mi | 28 Ravenswood Rd, London, E17 9LY
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python