When the search on the following website fails, there's a small modal dialog opening for the error, but the "id=noresultsfound" HTML I see in the source is not found by my code:
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
# now input search string
find_textbox = driver.find_element_by_id("search")
find_textbox.send_keys("ZQ")
find_textbox.send_keys(Keys.ENTER)
# check if any results found
# if not we have //*[@id="noresultsfound"]
try:
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.ID, "noresultsfound"))
)
finally:
driver.quit()
Of course the code of interest is what comes after the # check if any results found comment.
Of course I will also need to click on the OK button to dismiss it, but guess this will be feasible as well when I'll be able to find the modal window.
Code is runnable as it is.
The Ok element is within a Modal Dialog Box. To click() on the clickable element need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.ui-dialog-buttonset span.ui-button-text"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='ui-button-text' and text()='Ok']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC