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

Question:

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
Asked By: alwayshope430

||

Answers:

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']")
    
Answered By: undetected Selenium

The problem is that you have '''' in this line:

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')

This closes your initial string literal (which began with ') and starts a new string literal (which begins with '''). You never close this second string literal, so the rest of your code is a string, and Python complains it got to the end of the program without you ever closing the string.

I don’t have any idea what you’re trying to do with the four apostrophes in a row so I can’t tell you how to fix it, but that’s the problem.

Answered By: kindall
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')

That line of code contains a triple quote, which is never terminated.

Triple quotes are special in Python.

Answered By: John Gordon