0

I am attempting to scrape the website to eventually return back the "+2.5" value in the span class that is shown but am having a lot of issues getting the CSS SELECTOR to work properly and return that value.

I know this is far off but this was the best I could come up with but returned way too many values and made it impossible to sort:

driver.find_elements(By.CSS_SELECTOR, '.book-cell__odds > span')

Any help would be greatly appreciated

1

I have tried a number of different setups for the CSS SELECTOR but have not found any that are working the way I need

JeffC
  • 22,180
  • 5
  • 32
  • 55
mikebux
  • 11
  • 2
  • Could you please share more infos , which website are you scraping or what is your code. But generally you have to use class attribute. (BY.CSS_SELECTOR, 'div.book-cell(it doesn't seem in picture)) – Elkhan Mar 04 '23 at 07:23
  • Screenshots of the UI are great, screenshots of code or HTML are not. Please read why [a screenshot of code/HTML is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the relevant HTML and properly format it instead. – JeffC Mar 04 '23 at 16:36

2 Answers2

0

It's hard to say without seeing the whole page but one issue I see with your current locator is that you will return both SPANs in the posted HTML instead of just the first that contains "+2.5". You can further focus your CSS selector to just get the first span adding :first-of-type.

driver.find_elements(By.CSS_SELECTOR, '.book-cell__odds > span:first-of-type')

If that still doesn't get you what you want, you'll probably have to provide a link to the page.

More info on :first-of-type.

JeffC
  • 22,180
  • 5
  • 32
  • 55
0

Given the HTML:

html

The value +2.5 is within the first desendant <span> of it's parent <div>


Solution

To print the text +2.5 ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR, first-child and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.book-cell__odds > span:first-child"))).text)
    
  • Using CSS_SELECTOR, first-of-type and get_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.book-cell__odds > span:first-of-type"))).get_attribute("innerHTML"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352