簡體   English   中英

XPath 錯誤:[contains(text()="something")]' 不是有效的 XPath 表達式

[英]XPath error: [contains(text()="something")]' is not a valid XPath expression

在觀看了關於 XPath 的非常基本的教程之后,我第一次使用 selenium 構建了一個應用程序。 該應用程序包含卡片的獎金計算器。 我寫了這段代碼:

from selenium.webdriver import Chrome
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

cardmarket_url = "https://www.cardmarket.com/it/"

print("\nWhat are you searching for?\n\n")
index_product = 0
products = ["Yu-Gi-Oh", "Pokémon", "Magic", "Dragon Ball Super", "Flesh And Blood", 
            "Digimon", "Force of Will", "Final Fantasy", "World of Warcraft",
            "Star Wars Destiny", "Weiss Schwarz", "Dragoborne", "My Little Pony",
            "The Spoils"]

while (index_product <= 13):
    print(f"  [{str(index_product+1)}] - {prodotti[index_product]}")
    index_product = index_product + 1

gonext = False

while (gonext == False):
    try:
        product = products[int(input("\nInsert the product's number: "))-1]
        gonext = True
    except:
        print("You can only insert numbers beetween 1 and 14.")

print(f"\nYou selected {product}.")

if product == "Yu-Gi-Oh":
    product = "YuGiOh"
if product == "Pokémon":
    product = "Pokemon"
if product == "Force of Will":
    product = "FoW"
if product == "World of Warcraft":
    product = "WoW"
if product == "Spoils":
    product = "Spoils"

chrome_driver = ChromeDriverManager().install()
driver = Chrome(service=Service(chrome_driver))
driver.get(cardmarket_url+product.replace(" ", ""))

searched = False

while (searched == False):
    product = input("\nInsert card's name: ")
    rarity = input("Insert card's rarity: ")
    if product != "":
        searched = True
    else:
        print("Insert product's name.")

product = product+" "+rarity

driver.find_element(By.ID, "ProductSearchInput").send_keys(product)
driver.find_element(By.ID, "search-btn").click()
driver.find_element(By.XPATH, '//div[@class, "col-12 col-md-8 px-2 flex-column"]/a[contains(text()="'+product+'")]').click()

input()

好吧,倒數第二行給了我這個錯誤:

Traceback (most recent call last):   File "C:\Users\Utente\OneDrive\Documenti\Progetti_Python\KivyApp\SeleniumCardFinder\CardFinder.py", line 59, in <module>
    driver.find_element(By.XPATH, '//div[@class, "col-12 col-md-8 px-2 flex-column"]/a[contains(text()="'+prodotto+'")]').click()   File "C:\Users\Utente\OneDrive\Documenti\Progetti_Python\KivyApp\kivy_venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 857, in find_element
    return self.execute(Command.FIND_ELEMENT, {   File "C:\Users\Utente\OneDrive\Documenti\Progetti_Python\KivyApp\kivy_venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 435, in execute
    self.error_handler.check_response(response)   File "C:\Users\Utente\OneDrive\Documenti\Progetti_Python\KivyApp\kivy_venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //div[@class, "col-12 col-md-8 px-2 flex-column"]/a[contains(text()="Mewtwo GX")] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[@class, "col-12 col-md-8 px-2 flex-column"]/a[contains(text()="Mewtwo GX")]' is not a valid XPath expression.   (Session info: chrome=103.0.5060.114) Stacktrace: Backtrace:
        Ordinal0 [0x00796463+2188387]
        Ordinal0 [0x0072E461+1762401]
        Ordinal0 [0x00643D78+802168]
        Ordinal0 [0x00646754+812884]
        Ordinal0 [0x00646612+812562]
        Ordinal0 [0x006468A0+813216]
        Ordinal0 [0x00671595+988565]
        Ordinal0 [0x00671B1B+989979]
        Ordinal0 [0x0069E912+1173778]
        Ordinal0 [0x0068C824+1099812]
        Ordinal0 [0x0069CC22+1166370]
        Ordinal0 [0x0068C5F6+1099254]
        Ordinal0 [0x00666BE0+945120]
        Ordinal0 [0x00667AD6+948950]
        GetHandleVerifier [0x00A371F2+2712546]
        GetHandleVerifier [0x00A2886D+2652765]
        GetHandleVerifier [0x0082002A+520730]
        GetHandleVerifier [0x0081EE06+516086]
        Ordinal0 [0x0073468B+1787531]
        Ordinal0 [0x00738E88+1805960]
        Ordinal0 [0x00738F75+1806197]
        Ordinal0 [0x00741DF1+1842673]
        BaseThreadInitThunk [0x75436739+25]
        RtlGetFullPathName_UEx [0x77068FEF+1215]
        RtlGetFullPathName_UEx [0x77068FBD+1165]

改變

//div[@class,"col-12 col-md-8 px-2 flex-column"]/a[contains(text()="Mewtwo GX")]
            ^                                                     ^

//div[@class="col-12 col-md-8 px-2 flex-column"]/a[contains(text(),"Mewtwo GX")]
            ^                                                     ^

消除您的語法問題,但請注意

a[contains(text(),"Mewtwo GX")]

可能無法達到您的預期。 改為考慮

a[normalized-space()="Mewtwo GX"]

或者

a[contains(.,"Mewtwo GX")]

取決於您是否希望測試錨文本是否等於某些文本或包含某些文本的子字符串。

也可以看看

我需要驗證它是否包含某些文本的子字符串,然后我嘗試了'//div[@class="col-12 col-md-8 px-2 flex-column"]/a/text()[contains(.,"'+prodotto+'")]'我也試過'//div[@class="col-12 col-md-8 px-2 flex-column"]a/text()[contains(.,"'+prodotto+'")]' ,但它給了我錯誤..一個很長的錯誤,錯誤的最后一行是

The result of the xpath expression "//div[@class="col-12 col-md-8 px-2 flex-column"]/a/text()[contains(.,"Mewtwo GX ")]" is: [object Text]. It should be an element.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM