首页 > 解决方案 > 硒。x 轮播中数据的路径

问题描述

我希望下面的脚本打印一堆车名

我想我没有正确指向旋转木马。

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

driver = webdriver.Chrome(executable_path=r"../Downloads/chromedriver.exe")
driver.get('https://www.nationwidevehiclecontracts.co.uk/?msclkid=22db17e69fc512c5ab9f49993f841304&utm_source=bing&utm_medium=cpc&utm_campaign=B%20-%20NVC%20Brand%20-%20(Exact)&utm_term=nationwide%20car%20leasing&utm_content=Brand%20Variations')
carousel_x_path="/section[@class='section section--widget section--full-carousel section__heading-scroll variant-display']"
WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH,carousel_x_path)));

for name in (driver.find_elements_by_xpath(carousel_x_path+"/span[@class='make']")):
    print("it found a name")
    print(name.get_attribute("textContent"))

我得到这个错误。

Traceback (most recent call last):
  File "C:\Users\User\Desktop\miniiiiii.py", line 10, in <module>
    WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH,carousel_x_path)));
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

你能告诉我有什么问题吗

标签: pythonseleniumxpath

解决方案


请检查以下解决方案

driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.get('https://www.nationwidevehiclecontracts.co.uk/?msclkid=22db17e69fc512c5ab9f49993f841304&utm_source=bing&utm_medium=cpc&utm_campaign=B%20-%20NVC%20Brand%20-%20(Exact)&utm_term=nationwide%20car%20leasing&utm_content=Brand%20Variations')
driver.maximize_window()
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//body[@name='top']/section[@id='hotdeals']")))
links = driver.find_elements_by_xpath("//body[@name='top']/section[@id='hotdeals']/div[@id='offer-carousel']/div[@class='slick-list draggable']//h3//a")
for name in links:
     print name.text

注意:请在您的解决方案中添加以下导入

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

推荐阅读