Following code runs as expected if manually stepped in debug while it misbehaves if just run.
Aside from some initial code to reach the search page, I am then requesting a value which I know does not exist ("ZQZZQ") and thus the code should branch into the last else statement.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(
"/Users/bob/Documents/work/AIFA/scraper/scrape_gu/chromedriver"
)
# navigate through the initial agreement screens
driver.get("https://farmaci.agenziafarmaco.gov.it/bancadatifarmaci/cerca-farmaco")
readunderstood = driver.find_element_by_id("conf")
readunderstood.click()
accept = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "/html/body/div[5]/div[3]/div/button"))
)
accept.click()
# end of the initial agreement screens and general preparation
##############################################################
string_to_search = "ZQZZQ" # we can safely assume this does not exist
find_textbox = driver.find_element_by_id("search")
find_textbox.clear() # after the first search the old value will still be there
find_textbox.send_keys(string_to_search)
find_textbox.send_keys(Keys.ENTER)
# check if any results found
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.ID, "noresultsfound"))
)
# if we did retrieve something we don't have an error message
if element.text != "Nessun risultato trovato":
# first we have to find out the total number of retrieved pages
print(f"Digram {string_to_search} found!")
else:
print(f"Digram {string_to_search} not found. Need to close modal window!")
# click on ok of name not found modal dialog
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "/html/body/div[4]/div[3]/div/button"))
).click()
print("Modal window hopefully closed")
When I run it at full speed though I find it goes into the "if" branch, printing "Digram ZQZZQ found!" which is wrong.
Is the WebDriverWait call wrong? Thank you