Search code examples
pythonselenium-webdriverxpathsyntaxsyntax-error

SyntaxError: EOF while scanning triple-quoted string literal within XPath using Selenium and ChromeDriver?


I am trying to gather recent NFT transaction activity from an OpenSea profile. I am getting a SyntaxError for some reason and I cannot figure out what is causing the error. I have triple checked all of my code and am lost on where the error is coming from. Any advice would be greatly appreciated. I am on MacOS.

from selenium import webdriver
from selenium.webdriver.common.by import By

PATH = ('/Users/Chris/Desktop/chromedriver')
driver = webdriver.Chrome(PATH)
driver.get("https://opensea.io/GaryVee")
action = driver.find_element(By.CLASS_NAME, 'Blockreact__Block-sc-1xf18x6-0 Flexreact__Flex-sc-1twd32i-0 ''''FlexColumnreact__FlexColumn-sc-1wwz3hp-0 eaQZLu jYqxGr ksFzlZ')

for action in action:
    nft_name = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div[''1]/button/div/div[2]/div/div/div/div[2]/span[1]/div/div/a').text
    nft_id = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div[''1]/button/div/div[2]/div/div/div/div[2]/span[2]/a/div').text
    price_eth = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div[''1]/button/div/div[3]/div/div[1]/div/div[2]/text()').text
    price_usd = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div[''1]/button/div/div[3]/div/div[2]/span/div/div').text
    sending_user = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div[''1]/button/div/div[5]/div/a/span').text
    recieving_user = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[''2]/div[1]/button/div/div[6]/div/a/span').text
    status = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div[''1]/button/div/div[1]/h6')

And this is my error code:

  File "/Users/Chris /PycharmProjects/pythonProject/main.py", line 28
        status = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div[''1]/button/div/div[1]/h6')
                                                                                                                                            ^
SyntaxError: EOF while scanning triple-quoted string literal
Process finished with exit code 1

Solution

  • Seems the triple quotes within the CLASS_NAME was unnecessary or a typo. However, you can't pass multiple classnames through driver.find_element(By.CLASS_NAME, 'classname').

    To identify the element you can use either of the following Locator Strategies:

    • Using css_selector:

      action = driver.find_element(By.CSS_SELECTOR, ".Blockreact__Block-sc-1xf18x6-0.Flexreact__Flex-sc-1twd32i-0.FlexColumnreact__FlexColumn-sc-1wwz3hp-0.OpenSeaPagereact__DivContainer-sc-65pnmt-0.dBFmez.jYqxGr.ksFzlZ.iRiGb")
      
    • Using xpath:

      action = driver.find_element(By.XPATH, "//*[@class='Blockreact__Block-sc-1xf18x6-0 Flexreact__Flex-sc-1twd32i-0 FlexColumnreact__FlexColumn-sc-1wwz3hp-0 OpenSeaPagereact__DivContainer-sc-65pnmt-0 dBFmez jYqxGr ksFzlZ iRiGb']")