In this code, im trying to get and save links and then open them, once open, i want to press a button that sends me to another page and i want to press a button that sends me to the page i want, the problem is i have to do this 500 times and more, so i automate it but the problem comes now, if i do the code to press the button, it says i could’nt find it, this is not my fault, the css selected is well written, i dont now if if a selenium thing, this is my whole code
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
import time
# funcion para iniciar el navegador y configurarlo
def prepare_browser(waiting_time=5):
# ruta al perfil de usuario de firefox para cargar cookies
profile_path = "C:\Users\donal\AppData\Roaming\Mozilla\Firefox\Profiles\1om9itxe.default-release"
# darle opciones al navegador
options = Options()
options.add_argument(f"-profile")
options.add_argument(profile_path)
driver = webdriver.Firefox(options=options)
# darle la opcion de iniciar en pantalla completa
driver.maximize_window()
driver.get("https://nintendoproject.com/vip/catalogo-completo-nintendo-switch/")
time.sleep(waiting_time)
return driver
# guardar la devolucion de la funcion
driver = prepare_browser()
# funcion para buscar los links y sacar el nombre de los juegos
# junto con los links y mas informacion de ellos
def get_games_info(driver, waiting_time=5):
# sacar los links que hay en la pagina
game_links = driver.find_elements(By.CSS_SELECTOR, "div > b > span > a")
# bucle para recorrer todos los links y hacerlos textos
with open("first_links.txt", "a+", encoding="utf-8") as file:
# ir al inicio del archivo
file.seek(0)
# leer todas las lineas del archivo y guardarlas en una variable
existing_links = file.readlines()
# abrir un nuevo archivo para escribir los nuevos enlaces
with open("second_links.txt", "a+", encoding="utf-8") as second_file:
# ir al incio del archivo
second_file.seek(0)
# leer los links de este archivo
second_file_read_links = [second_file_read_link.strip() for second_file_read_link in second_file.readlines()]
# iterar sobre los enlaces para abrirlos
for link in existing_links:
link = link.strip()
driver.get(link)
# clase del boton para ir a la siguientep pagina
next_page_button_location = ".elementor-button.elementor-button-link.elementor-size-sm.elementor-animation-float"
# esperar {waiting_time} para que cargue la pagina
time.sleep(waiting_time)
# ubicar el boton y darle click
next_page_button = driver.find_element(By.CSS_SELECTOR, next_page_button_location)
next_page_button.click()
# esperar {waiting_time} para que cargue la pagina
time.sleep(waiting_time)
# localizar las etiquetas a dentro de span para sacarles el enlace
game_download_links = driver.find_element(By.TAG_NAME, "a")
# esperar {waiting_time} para que cargue la pagina
time.sleep(waiting_time)
# sacar el atributo
game_download_link_attributes = game_download_links.get_attribute("href")
print(game_download_link_attributes)
if game_download_link_attributes not in second_file_read_links:
second_file.write(game_download_link_attributes)
get_games_info(driver)
driver.quit()
the thing is that in a separate file it works, this is the other code
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
import time
profile_path = "C:\Users\donal\AppData\Roaming\Mozilla\Firefox\Profiles\1om9itxe.default-release"
# darle opciones al navegador
options = Options()
options.add_argument(f"-profile")
options.add_argument(profile_path)
driver = webdriver.Firefox(options=options)
driver.get("https://nintendoproject.com/13-xiii-remake-rom-xci-nsenlaces-vip/")
time.sleep(5)
button = driver.find_element(By.CSS_SELECTOR, ".wp-block-button__link.wp-element-button")
attr = button.get_attribute("href")
print(attr)
button.click()
time.sleep(5)
current_url = driver.current_url
print(current_url)
driver.quit()
i dont know why it works in this separate file, i tried 3 things, the first one was to take the new url with driver.current_url but it takes the previous one, not the new one, the second was to take the button href that sends me to the page i want to go, and last one open the link by pressing the button, but none of them work in the main file, just in this file, why!!!? i spent 4 hours on this
karen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.