元素与硒和python无法相互作用的问题

发布于 2025-02-03 06:19:54 字数 2323 浏览 3 评论 0原文

我的硒是我的新手,而且我一直在运行几个非常小的网络刮擦项目。 当我尝试通过.click()函数单击此元素时,我一直在获得“不相互作用”的

html部分,就是这样:

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

我的python代码看起来像这样:

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

所有其他部分都会发送我需要的信息,但是当我检查它时,条件箔是正确的,因为该元素不可交互,因此会跳至异常。

我尝试使用 CSS_SELECTOR 进行操作,并且使用常规 XPath ,我看到了他们建议使用 full xpath 的另一个答案修复了问题,但行不通。

我该怎么办?

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="{"name":"finish","value":"Foil"}" href="https://starcitygames.com/search/?card_name=Glimmervoid&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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2025-02-10 06:19:54

因此,我弄清楚了如何为我想要的元素获取HREF,这很简单,就像获取该元素,然后告诉我的代码转到该页面并执行其余的代码:

这就是现在的样子:

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)

现在移动下一步。谢谢大家!

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!

も星光 2025-02-10 06:19:54

browser_options.add_argument("headless")

应该是

browser_options.add_argument("--headless")

您需要先滚动到每张卡,然后再拿到价格。

以下是示例代码:

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)

输出:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文