The problem really is that you click on the div
, not on the link it contains. You can select the <div>
by id
as you do now and then select the element <a>
from it, for example using XPATH.
Later, if your ultimate goal is to get the phone as I suppose, you have to wait for the call to the JavaScript function to be processed. Since at this point the previous hyperlink disappears, we can use it with an wait implicit to know when the text is available with the phone.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
timeout = 10
driver = webdriver.Firefox()
driver.get("http://www.compraensanjuan.com/sitio/73744/ricardo-monte/9/1/1")
try:
wait = WebDriverWait(driver, timeout)
div = wait.until(EC.presence_of_element_located((By.ID, 'bt')))
wait = WebDriverWait(div, timeout)
wait.until(EC.presence_of_element_located((By.XPATH, ".//a"))).click()
wait.until(EC.invisibility_of_element_located((By.XPATH, './/a')))
except TimeoutException:
print("Tiempo de espera superado")
else:
print("El teléfono es {}".format(div.text))
driver.close()
You can directly select the element a using XPATH in several ways, for example with:
"//div[@id='bt']//a"
But since we need the div
to get the phone at the end, we make up for it in two steps.