-1

i try to get the content of

<span class="noactive">0&nbsp;Jours restants</span>

(which is the expiration date of the warranty) but i don't know how to get it (i need to print it in a file)

my code

def scrapper_lenovo(file, line):
   CHROME_PATH = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
   CHROMEDRIVER_PATH = 'C:\webdriver\chromedriver'
   WINDOW_SIZE = "1920,1080"
   chrome_options = Options()  
   chrome_options.add_argument("--headless")
   chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
   chrome_options.binary_location = CHROME_PATH
   d = driver.Chrome(executable_path=CHROMEDRIVER_PATH,
                          chrome_options=chrome_options)  
   d.get("https://pcsupport.lenovo.com/fr/fr/warrantylookup")
   search_bar = d.find_element_by_xpath('//*[@id="input_sn"]')
   search_bar.send_keys(line[19])
   search_bar.send_keys(Keys.RETURN)
   time.sleep(4)
   try:
      warrant = d.find_element_by_xpath('//*[@id="W-Warranties"]/section/div/div/div[1]/div[1]/div[1]/p[1]/span')
      file.write(warrant)
   except:
      print ("test")
      pass
   if ("In Warranty" not in d.page_source):
    file.write(line[3])
    file.write("\n")
   d.close()

i tried as you can see to print the content of 'warrant' and i couldn't find any function allowing it (i saw some using .text(), .gettext() but for whatever reasons i couldn't get them to work).

1 Answers1

-1

You could try explicitly matching the required tag, the relevant XPath expression would be:

//span[@class='noactive']

I would also recommend getting of this time.sleep() function, it's some form of a performance anti-pattern, if you need to wait for presence/visibility/invisibility/absence of a certain element you should rather go for Explicit Wait

So remove these lines:

time.sleep(4)
warrant = d.find_element_by_xpath('//*[@id="W-Warranties"]/section/div/div/div[1]/div[1]/div[1]/p[1]/span')

and use this one instead:

warrant = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, "//span[@class='noactive']")))

More information: How to use Selenium to test web applications using AJAX technology

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Presence is just that the element is in the DOM but not necessarily visible. If you are going to pull the text, the element needs to be visible so you would want `visibility_of_element_located`. Also, OP wants the text from the element so you need to add `.text`. – JeffC Aug 08 '19 at 17:26