Reputation: 1238
I am new to using Python with Selenium, and I am having trouble with scraping a code from a web.
I don't want anybody to fix it for me. I am looking for a hand on what the problem might be, so that I can proceed.
# inicializar el paketito selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://www.codigosdescuento.com/categoria-accesorios_de_moda.html")
boton_promo = driver.find_element_by_xpath("//a[contains(@class,'boton_descuento')][1]")
boton_promo.click()
#buscamos el codigo
try:
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='rasca_cupon']")))
except:
print("K va... no se ha encontrado el codigo")
raise SystemExit
codigo_descuento = driver.find_element_by_xpath("//div[@class='rasca_cupon']")
print(codigo_descuento.text)
It prints the exception, even though the expected element exists and gets visible.
How can I know what makes the driver not see the element?
Upvotes: 0
Views: 315
Reputation: 12930
Selenium provides just API to Automate browser, it doesn't do automatically. so you need to pay attenstion to what you are doing manually and need to write code to Automate exact steps.
In your case, when you click on link to see coupon it opens a new tab(browser window) and then shows the coupon there. You gotta write that code in Automation as well.
Following code is working perfectly after adding switch_to_window()
driver = webdriver.Firefox()
driver.get("http://www.codigosdescuento.com/categoria-accesorios_de_moda.html")
boton_promo = driver.find_element_by_xpath("//a[contains(@class,'boton_descuento')][1]")
boton_promo.click()
driver.switch_to_window(driver.window_handles[-1])
WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='rasca_cupon']")))
codigo_descuento = driver.find_element_by_xpath("//div[@class='rasca_cupon']")
print(codigo_descuento.text)
Upvotes: 1