简体   繁体   中英

Selenium, Unable to click on JavaScript button

I have checked on stack overflow and other websites to find the solution to my problem but nothing is working. p This was the closest solution I could fine but it didn't work How to click href javascript:void(0) and hidefocus=true with selenium python?

import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.common.action_chains import ActionChains

if __name__ == '__main__':
    driver = uc.Chrome(version_main=106)
    driver.get("https://sneakernews.com/air-jordan-release-dates/")
    #
    # WebDriverWait(driver, 30).until(
    #     EC.presence_of_element_located((By.CSS_SELECTOR, "div[class='content-box']"))
    #
    # )
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='sneaker-release-load-more-btn']"))).click()
    WebDriverWait(driver, 30).until(
        EC.presence_of_element_located((By.ID, ".//div[@class='more-button']/div/a[@id='sneaker-release-load-more-btn']")))
    jordan_names = driver.find_elements(By.ID,".//div[@class='more-button']/div/a[@id='sneaker-release-load-more-btn']")
print(jordan_names)

Could you please give me a direct solution that actually works for this particular example because I have spent many hours on this.

You can attempt to click on the element using a JavaScript click - running a JavaScript click may or may not work depending on how the website is set up.

if __name__ == '__main__':
    driver = uc.Chrome(version_main=106)
    driver.get("https://sneakernews.com/air-jordan-release-dates/")
    clickable_element = driver.find_element(By.XPATH, "//a[@id='sneaker-release-load-more-btn']")
    driver.execute_script("arguments[0].click();", clickable_element)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM