【问题标题】:selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"h2"}selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“h2”}
【发布时间】:2021-11-08 15:13:57
【问题描述】:

在下面的代码中,我无法转到下一页。如果有人可以帮助我,将会有很大的帮助。我正在用 Python 编写这个脚本并使用 selenium chrome webdriver。

我在页面末尾出现错误。 引发异常类(消息、屏幕、堆栈跟踪) selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“method”:“css selector”,“selector”:“h2”} (会话信息:chrome=93.0.4577.63)

import xlwt
from selenium import webdriver
import re
import time
from datetime import date
class kotsovolosmobiles:
    def __init__(self):
        self.url='https://www.kotsovolos.gr/mobile-phones-gps/mobile-phones/smartphones'
        self.country='GR'
        self.currency='euro'
        self.VAT= 'Included'
        self.shipping = 'Available for shipment'
        self.Pre_PromotionPrice ='N/A'
    def kotsovolos(self):        
        wb = xlwt.Workbook()
        ws = wb.add_sheet('Sheet1',cell_overwrite_ok=True)
        ws.write(0,0,"Product_Url")
        ws.write(0,0,"Product_Manufacturer")
        ws.write(0,1,"Product_Url")
        ws.write(0,2,"Product_Price")
        ws.write(0,3,"Product_Model")
        ws.write(0,4,"Memory")
        ws.write(0,5,"Currency")
        ws.write(0,6,"Color")
        ws.write(0,7,"VAT")
        ws.write(0,8,"Shipping Cost")
        ws.write(0,9,"Pre-PromotionPrice")
        ws.write(0,10,"Country")
        ws.write(0,11,"Date")
        ws.write(0,12,"Raw_Model")
        wb.save(r"C:\Users\Karthick R\Desktop\VS code\kotsovolos.xls")
        driver=webdriver.Chrome()            
        driver.get(self.url)
        today = date.today()
        time.sleep(5)
        cookies = driver.find_element_by_css_selector('a[id="CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll"]')
        cookies.click()
        print("cookies accepted")
        driver.maximize_window() 
        time.sleep(5)
        titles = []
        models = []
        memorys = []
        prod_prices = []
        p_links =[]
        p_colors = []  
        x = 15 
        while True: 
            storage_box = []          
            storage_box = driver.find_elements_by_css_selector('div[class="product"]')
            for storage_boxes in storage_box:
                product_url = storage_boxes.find_element_by_tag_name('h2')
                product_urls = product_url.find_element_by_tag_name('a').get_attribute('href')
                print(product_urls)
                p_links.append(product_urls)

                p_model = storage_boxes.find_element_by_css_selector('div[class="title"] a').text
                print(p_model)
                models.append(p_model)

                manufacturer1 = p_model.split(" ")
                print(manufacturer1[0])
                titles.append(manufacturer1[0])

                memory = []
                memory = re.findall('\d+ ?[gG][bB]',p_model)
                print(memory)
                memory1 = str(memory).replace("['",'').replace("']",'').replace("[]",'').strip()
                if "," in memory1:
                    arr=memory1.split(",")
                    for str1 in arr:
                        str2=str1.replace("GB", "").replace("gb", "").replace("'", "").strip() 
                        if len(str2)!=1: 
                            memory_str=str1
                            break 
                elif (memory1 == ""):
                    memory_str ='N/A'
                else:
                    memory_str=memory1 
                memory_str = memory_str.replace("'", "").strip() 
                print(memory_str)
                memorys.append(memory_str)

                colors= []
                prod_color = p_model.split(" ")
                length = len(prod_color)
                indexcolor = length-3 
                colors.append(prod_color[indexcolor])
                color1 = str(colors).replace("['",'').replace("']",'').strip()
                print(color1)
                p_colors.append(color1)

                p_price = storage_boxes.find_element_by_css_selector('.priceWithVat > .price').text
                print(p_price)
                prod_prices.append(p_price)
            next = driver.find_element_by_css_selector('.pagination_next a')
            url = next.get_attribute('href')
            driver.get(url)

kotsovolos_gr = kotsovolosmobiles()
kotsovolos_gr.kotsovolos()

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    错误很明显,在此css_selector 下没有带有h2 的标签

    div[class="product"]
    

    我建议将其替换为 a 标签。

    因为我在HTML DOM看到这个

    <a href="https://www.kotsovolos.gr/mobile-phones-gps/mobile-phones/smartphones/221351-apple-iphone-12-64gb-black" title="https://www.kotsovolos.gr/mobile-phones-gps/mobile-phones/smartphones/221351-apple-iphone-12-64gb-black" data-uw-rm-brl="false"><img src="https://assets.kotsovolos.gr/product/221351-m.jpg" alt="Apple iPhone 12 64GB Black" data-uw-rm-ima-original="apple iphone 12 64gb black"></a>
    

    在这个css_selector里面

    div[class="product"]
    

    此外,从技术上讲,您根本不需要h2。下面的代码应该会为您带来魔力。

    storage_box = driver.find_elements_by_css_selector('div[class="product"]')
        for storage_boxes in storage_box:
                    product_urls = driver.find_element_by_tag_name('a').get_attribute('href')
                    print(product_urls)
                    p_links.append(product_urls)
    

    另外,我不确定您是否正在寻找每个产品的标题,以防万一

    time.sleep(2)
    storage_box = driver.find_elements_by_xpath("//div[@class='product']")
    for storage_boxes in storage_box:
        time.sleep(1)
        product_url = storage_boxes.find_element_by_xpath(".//h2")
        product_urls = product_url.find_element_by_tag_name('a').get_attribute('href')
        print(product_urls)
        p_links.append(product_urls)
    

    【讨论】:

    • 不,它无法正常工作,因为我无法移动到下一页。
    • 使用第一次编码我没有获得网站链接,使用第二次编码我无法进入下一页。我的问题是我想循环页面直到结束并且需要抓取数据
    猜你喜欢
    • 2022-07-21
    • 2021-11-05
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    相关资源
    最近更新 更多