0

I am trying to extract market cap and 24H volume from a list of coins (e.g., [['https://crypto.com/price/bitcoin', 'https://crypto.com/price/ethereum', 'https://crypto.com/price/tether',] using selenium in python.

I am using the XPATH for the market cap and 24H volume but for some of the coins the XPATH is slightly different, so I need to search for two XPATHs at once. You can see the tiny difference in XPATHS below.

XPATH market-cap bitcoin:

//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[3]/div[1]/p

XPATH market-cap hex:

//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[4]/div[1]/p

I have tried the following:

    market_cap = driver.find_element(by=By.XPATH, value = "//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[3]/div[1]/p" | "//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[4]/div[1]/p").text
    dict_data['market cap'] = market_cap
    print(market_cap)
    volume = driver.find_element(by=By.XPATH, value = "//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[3]/div[2]/p" | "//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[4]/div[2]/p").text 
    dict_data["24H volume"] = volume
    print(volume)

But i get this error:

    market_cap = driver.find_element(by=By.XPATH, value = "//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[3]/div[1]/p" | "//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[4]/div[1]/p").text
TypeError: unsupported operand type(s) for |: 'str' and 'str'

I have also tried:

    market_cap = driver.find_element(by=By.XPATH, value = "//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[3]/div[1]/p" or "//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[4]/div[1]/p").text
    dict_data['market cap'] = market_cap
    print(market_cap)
    volume = driver.find_element(by=By.XPATH, value = "//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[3]/div[2]/p" or "//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[4]/div[2]/p").text 
    dict_data["24H volume"] = volume
    print(volume)

But it just searches for the first XPATH and not both, so when I get to the hex coin I get this error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='__next']/div[3]/div/div/div[3]/div[1]/div[1]/div[3]/div[1]/p"}

Lewis-010
  • 35
  • 5
  • 1
    Both logical **or** (`or`) and the **union** operator (`|`) are part of XPath and so should be used within the quotes delimiting your XPaths. As you're using them, they're being interpreted as part of Python, not XPath. See duplicate links for further details. – kjhughes Nov 04 '22 at 12:59

0 Answers0