I have been trying to scrape a webpage and found something odd. When I try Python selenium webdriver's find_element_by_xpath without time.sleep, I get nothing from the command. But if I add time.sleep, I suddenly get the information I intend to get.
I first notice this pattern, when I first run the code without time.sleep I get nothing. But I get the result when I run the same code one more time. So I tried adding a short break, and suddenly the code worked perfectly.
Here is the code without time.sleep
driver.get(link)
info = driver.find_element_by_xpath('//*[@id="page-number"]').text
print info
Here is the one with time.sleep
driver.get(link)
time.sleep(1)
info = driver.find_element_by_xpath('//*[@id="page-number"]').text
print info
I understand I am supposed to provide an actual website address to get the best answer. But I didn't want to reveal which website I am trying to web scrape.
Could someone explain to me theoretically why this might happen?