如何解决:“selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element”

问题描述 投票:0回答:1

我正在尝试学习 Selenium。我正在编写一个脚本来将多个项目添加到购物车。

如果我删除了课程,脚本允许我将商品添加到购物车中,但是当我将它放在一个课程下时,它给了我这个错误:

C:\Users\bocse\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\bocse\PycharmProjects\pythonProject\EMAG\Sample2.py 
Traceback (most recent call last):
  File "C:\Users\bocse\PycharmProjects\pythonProject\EMAG\Sample2.py", line 31, in <module>
    add_item("mouse logitech")
  File "C:\Users\bocse\PycharmProjects\pythonProject\EMAG\Sample2.py", line 26, in __init__
    addcart = driver.find_element("xpath","//*[@id=\"main-container\"]/section[3]/div/div[1]/div[2]/div/div[2]/div[2]/form/div/div[4]/div[2]/div[1]/button")
  File "C:\Users\bocse\PycharmProjects\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 830, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
  File "C:\Users\bocse\PycharmProjects\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "C:\Users\bocse\PycharmProjects\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="main-container"]/section[3]/div/div[1]/div[2]/div/div[2]/div[2]/form/div/div[4]/div[2]/div[1]/button"}
  (Session info: chrome=110.0.5481.178)
Stacktrace:
Backtrace:
    (No symbol) [0x002737D3]
    (No symbol) [0x00208B81]
    (No symbol) [0x0010B36D]
    (No symbol) [0x0013D382]
    (No symbol) [0x0013D4BB]
    (No symbol) [0x00173302]
    (No symbol) [0x0015B464]
    (No symbol) [0x00171215]
    (No symbol) [0x0015B216]
    (No symbol) [0x00130D97]
    (No symbol) [0x0013253D]
    GetHandleVerifier [0x004EABF2+2510930]
    GetHandleVerifier [0x00518EC1+2700065]
    GetHandleVerifier [0x0051C86C+2714828]
    GetHandleVerifier [0x00323480+645344]
    (No symbol) [0x00210FD2]
    (No symbol) [0x00216C68]
    (No symbol) [0x00216D4B]
    (No symbol) [0x00220D6B]
    BaseThreadInitThunk [0x75E900F9+25]
    RtlGetAppContainerNamedObjectPath [0x77E37BBE+286]
    RtlGetAppContainerNamedObjectPath [0x77E37B8E+238]
    (No symbol) [0x00000000]

这是我的代码:

from selenium import webdriver
from selenium.webdriver import Keys
import time


class add_item:

    def __init__(self, item):
        self.item = item

        driver = webdriver.Chrome()
        driver.get("https://www.emag.ro/")

        # Search for item
        search = driver.find_element("id", "searchboxTrigger")
        search.send_keys(self.item)
        search.submit()
        time.sleep(3)

        # Select first item
        select = driver.find_element("xpath", "//*[@id='card_grid']/div[1]")
        select.click()
        time.sleep(3)

        # Add into the cart
        addcart = driver.find_element("xpath","//*[@id='main-container']/section[3]/div/div[1]/div[2]/div/div[2]/div[2]/form/div/div[4]/div[2]/div[1]/button")
        addcart.click()
        time.sleep(3)


add_item("mouse logitech")
add_item("mouse razer")

如果我删除了类,脚本允许我将商品添加到购物车中,但是当我将其设为类时,我遇到了上述错误。有什么帮助吗?

python selenium-webdriver pycharm selenium-chromedriver
1个回答
0
投票

我不认为这个问题是由于类创建引起的。根据错误中的说明,抛出的异常是

NoSuchElementException
,因此找不到您试图定位的
WebElement

根据我的测试,它在您的第一个查询中运行良好

mouse logitech
,但是当涉及到您的第二个查询时
mouse razer
。您定义的要添加到购物车的 XPath:

//*[@id='main-container']/section[3]/div/div[1]/div[2]/div/div[2]/div[2]/form/div/div[4]/div[2]/div[1]/button

不完全正确。

这是因为在您的第二次查询中,网页中存在其他表格,该表格推荐某些产品。因此,您定义的

XPath
不再是排他的。

因此,您需要为添加到购物车按钮定义更加独特和独特的 XPath。

我试过:

addcart = driver.find_element("xpath", "//div[@class='product-buy-area-wrapper']//button")
addcart = driver.find_element("xpath", "//form[@class='main-product-form']//button[@type='submit']")

他们似乎工作得很好。

© www.soinside.com 2019 - 2024. All rights reserved.