0

I am scraping data from a lottery website to store all the past results in a database.

I got a problem that almost all information I want to retrive is at /html/body/div[3]/h2 per page of each date the lottery y drawn, but only one of them is at /html/body/div[2]/h2

To fix it I used the following code.

try:

      text = self.driver.find_element(By.XPATH, '/html/body/div\[3\]/h2').text

except :

       text = self.driver.find_element(By.XPATH, '/html/body/div\[2\]/h2').text

It works but I don't like it. Is there a better way to handle this kind of discrepancies?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Looks fine to me. This is what `try/except` blocks are for - see if one thing works, and if it doesn't, try something else. – MattDMo Mar 31 '22 at 23:11

1 Answers1

0

Simply use the <h2> tag and you can use either of the following locator strategies:

  • Using TAG_NAME:

    text = self.driver.find_element(By.TAG_NAME, "h2").text
    
  • Using CSS_SELECTOR:

    text = self.driver.find_element(By.CSS_SELECTOR, "h2").text
    
  • Using XPATH:

    text = self.driver.find_element(By.XPATH, "//h2").text
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352