Home > OS >  I keep getting NoSuchElementException while trying to access. What can I do?
I keep getting NoSuchElementException while trying to access. What can I do?

Time:05-27

So I'm trying to get into web scraping and web automation. I have just started with selenium so I'm a little bit lost on what to do. I have looked here for similar questions and so far none have worked so can the problem be?

Here's my code:

from selenium import webdriver                    
from selenium.webdriver.common.keys import Keys    
 


url = 'https://starcitygames.com/search/?search_query=Lantern of insight'

browser = webdriver.Chrome()
browser.get(url)
browser.maximize_window()
browser.implicitly_wait(20)

prices = browser.find_element_by_css_selector('div.hawk-results-item__options-table-cell hawk-results-item__options-table-cell--price childAttributes')
browser.switch_to.frame(prices)

for each in prices:
    p = each.text
    print(p) 

And I get the following Error message:

 raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div.hawk-results-item__options-table-cell hawk-results-item__options-table-cell--price childAttributes"} 

IF it helps the html of the element I'm trying to access is the following:

<div >.99</div>

So what can I change? I already switch from find_element_by_class to a css selector.

Thanks in advance for any help!

CodePudding user response:

There are a few problems with your approach.
First of all, the find_element_by_* functions are deprecated so you should use the find_element() functions instead. You'll need this import to do that.

from selenium.webdriver.common.by import By

Considering you are trying to loop through the found elements i suppose you meant to use find_elements() which returns a list of elements that match the given locator.
When using cssSelector, you need to specify the html tag then open [] brackets and specify the attribute.

In your case this should work

prices = browser.find_elements(By.CSS_SELECTOR, "div[class='hawk-results-item__options-table-cell hawk-results-item__options-table-cell--price childAttributes']")
  • Related