简体   繁体   中英

Element not interactable issue with Selenium and Python

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?

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM