[Python] JavascriptException: Message: javascript error: Cannot read property 'click' of...

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following code in which I enter a page and search for a product, I want to execute a JavaScript code

    from selenium import webdriver
    from getpass import getpass
    #-------------------------------------------PRODUCT SEARCH-----------------------------------------------------------------------------------
    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument("--disable-blink-features=AutomationControlled")

    driver = webdriver.Chrome("C:\\Users\\stbaz\\Documents\\Python\\ChromeTools\\chromedriver.exe", options=options)
    driver.get("https://www.innvictus.com/")
    product_textbox = driver.find_element_by_id("is-navigation__search-bar__input")
    product_textbox.send_keys("FW7093")
    product_textbox.submit()
    #------------------------------------------PRODUCT SEARCH END--------------------------------------------------------------------------------------

    driver.implicitly_wait(5)
    js='javascript:document.getElementsByClassName("buy-button buy-button--sticky buy-button--buy-now visible-xs visible-sm")[1].click();window.open("/checkout")'
    driver.execute_script(js)


    But I get the following error

    selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'click' of undefined


    I can run that code manually in chrome, I use a bookmark, but I want to run it in Python, what am I doing wrong?

    Continue reading...

Compartilhe esta Página