I want to scrape 'href' tags from a webpage which includes profile URL of the name searched on the website. Sometimes, it might even give a null result if the profile of a particular doesn't exist. I am using python selenium wherein I am inputting names from a csv file and sending the keys to the search bar on the website in a loop. But, sometimes randomly the profile URL of the previous search gets carried to the existing name search. This occurs very randomly and I have checked the logic of the code multiple times and there seems to be no error in that part.
I suspect that the webpage is not loading fully before I am pointing towards a particular element using selenium. I have tried using sleep() but it also works for some values and only sometimes. Increasing sleep time would only increase the time with no guarantee for the accuracy (tried and tested).
I actually want a way to check if the URL of the person exists on the webpage or not and if it does I want the url of that specific person and not of the previous one. Is there a solution to this. This is a small block of code which will add further clarity:
# unique result with name
name = '"' + row[1] + '"'
xpath = "//*[@class='search-result__image-wrapper']/a"
search_query.send_keys(name)
search_query.send_keys(Keys.RETURN)
sleep(5)
#WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.XPATH, xpath)))
links = driver.find_elements_by_xpath(xpath)
if len(links) == 1:
for link in links:
url = link.get_attribute('href')
print('name')
P.S.: I have also gone through similar questions on stack overflow but none of them seem to work. I have also used the web driver wait method, which checks for the availability of a specific element on the website that occurs on every search but that doesn't seem to work either.