0

I'm fairly new with Selenium and I've been running a couple of very small web scraping projects. When I try to click on this element through the .click() function I keep getting "Element not interactable"

The html section I'm trying to interact is this:

<a class="hawk-iconBefore hawk-styleCheckbox hawk-styleList" data-options="{&quot;name&quot;:&quot;finish&quot;,&quot;value&quot;:&quot;Foil&quot;}" href="https://starcitygames.com/search/?card_name=Glimmervoid&amp;finish=Foil" rel="nofollow"><span class="hawk-selectionInner">Foil <span class="hawk-facetCount">(5)</span></span></a>

And my python code looks like this:

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


url = 'https://starcitygames.com/'

card_name = 'Fatal Push'
expansion_name = 'Double Masters'
foil = True
card_price = 0

browser_options = webdriver.ChromeOptions()
browser_options.add_argument("headless")
browser = webdriver.Chrome(options=browser_options)
browser.get(url)
browser.implicitly_wait(0.2)
browser.maximize_window()

print(card_name)

def get_card_price():
    global card_price
    print("Finding card...")
    browser.find_element(By.CSS_SELECTOR, "[name='search_query']").send_keys(card_name)
    search_button = browser.find_element(By.CLASS_NAME, "search-submit")
    search_button.click()

    if foil:
        print("Checking if Foil...")
        foil_select = browser.find_element(By.XPATH, "/html/body/div/div[1]/main/aside/div[2]/div[2]/div/div[5]/div/ul/li[1]/a")
        try:
            foil_select.click()
            print("It's Foil")
        except:
            print("Element not interactable")

    cards = browser.find_elements(By.CLASS_NAME,"hawk-results-item")
    for card in cards:
        c = card.text
        price = card.find_element(By.CSS_SELECTOR, "div[class='hawk-results-item__options-table-cell hawk-results-item__options-table-cell--price childAttributes']")

        if expansion_name in c:
            card_price = price.text
    return card_price

get_card_price()
print("Fetching card price...")
print(card_price)


browser.quit()

All other part send the info I need but when I check it the condition foil is true it jumps to the exception due to the element not being interactable.

I have tried accesing it with css_selector, and with the regular xpath, I saw another answer in which they suggested using the full XPATH and that it fixed the issue but it didn't work.

What could I do?

2
  • Are you sure that the element you select foil_select is the correct element ? Did you check its attributes ? Also, to work around this issue you could try to just find the element, get the link and tell your browser to change your current page. Commented May 31, 2022 at 16:04
  • To be honest I am not entirely sure if it's the correct one since it's inside a list but I have tried accessing the parent element as well as the span that it's after. Also I have tried looking on how to get the link and change the page but I haven't find it in the documentations for selenium, any clues where I can get that? Commented May 31, 2022 at 16:07

2 Answers 2

1

So I figured out how to fetch the href for the element I wanted and it was as simple as just getting that and then telling my code to go to that page and execute the rest of the code:

That's how it looks now:

if foil:
        print("Checking if Foil...")
        try:
            foil_select=browser.find_element(By.XPATH, '//*[@id="hawkfacet_finish"]/li[1]/a')
            link = foil_select.get_attribute("href")
            print("It's Foil")
            browser.get(link)
        except:
            print("Element not interactable")
    else:
        foil_select=browser.find_element(By.XPATH, '//*[@id="hawkfacet_finish"]/li[2]/a')
        link = foil_select.get_attribute("href")
        print("It's not foil")
        browser.get(link)

Now to move on with the next step. Thanks everyone!

Sign up to request clarification or add additional context in comments.

Comments

0

This

browser_options.add_argument("headless")

should be

browser_options.add_argument("--headless")

You need to scroll to each cards first before grabbing the price.

Below is the sample code:

driver.maximize_window()
wait = WebDriverWait(driver, 20)

url = 'https://starcitygames.com/'

card_name = 'Fatal Push'
expansion_name = 'Double Masters'
foil = True
card_price = 0

#browser_options = webdriver.ChromeOptions()
#browser_options.add_argument("headless")
#browser = webdriver.Chrome(options=browser_options)
driver.get(url)
driver.implicitly_wait(0.2)
driver.maximize_window()

print(card_name)

def get_card_price():
    global card_price
    print("Finding card...")
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='search_query']"))).send_keys(card_name)
    search_button = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "search-submit")))
    search_button.click()

    if foil:
        print("Checking if Foil...")
        foil_select = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul#hawkfacet_rarity li a[data-options*='Rare']")))
        try:
            foil_select.click()
            print("It's Foil")
        except:
            print("Element not interactable")
    time.sleep(5)
    cards = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='hawk-results-item']")))
    for card in cards:
        driver.execute_script("arguments[0].scrollIntoView(true);", card)
        c = card.get_attribute('innerText')
        print(c)
        price = card.find_element(By.XPATH, ".//descendant::div[contains(@class, 'price childAttributes')]")
        print(price.text)
        if expansion_name in c:
            card_price = price.text
    return card_price

get_card_price()
print("Fetching card price...")
print(card_price)

Output:

Fatal Push
Finding card...
Checking if Foil...
It's Foil
Fatal Push (Borderless)

Double Masters - Variants

Near Mint -
English
$14.99
QTY: 0
NOTIFY ME
$14.99
Fatal Push (Borderless)

Double Masters - Variants (Foil)

Near Mint -
English
$14.99
QTY: 3
Add to cart
$14.99
Fetching card price...
$14.99

Process finished with exit code 0

4 Comments

Thanks for the suggestion! Wouldn't your idea fetch the price for each item? I figured that if I fetch the href for the item I want I'll get just the specific price for the one card I want. Also, does your idea might run faster than the one I published? I was planning on optimizing a little bit after solving this.
Well you are getting a href from an anchor tag, and you are traversing to that url, I think you will likely get stale element exception, for larger data. My code is already optimized with explicit waits. Eventually you will have to switch to explicit wait.
Moreover in your original post, you never mentioned that you would look for an href, original post was all about element not interactable error.
You're right. I decided to try that approach (href looking) as another user suggested but I will definetely try your solution as soon as I can. Thanks for the input!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.