Why does not selenium takes the attribute from the selected tag but it does in a separate file?

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># 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()
</code>
<code># 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() </code>
# 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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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()
</code>
<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() </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

New contributor

karen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật