Kryptic Coconut
Kryptic Coconut

Reputation: 279

Cant click button with selenium

I was making a webscraper with selenium and bs4 to keep some shopping items' stock in track, there is a load more button I want to click which looks like this button and here is the HTML code for it, the website is https://www.nvidia.com/en-us/shop/geforce/?page=1&limit=9&locale=en-us

HTML code of the button

HTML code of the button

Whenever I try to find the element with bs4 it works but it never works with

driver.find_element_by_class_name("buy-link load-more-btn")

and I need it to click the button, can someone help me out

Am I was passing too many classes at once?

Upvotes: 0

Views: 545

Answers (4)

undetected Selenium
undetected Selenium

Reputation: 193308

The LOAD MORE element is located at the bottom of the DOM Tree so to click on it you need to induce WebDriverWait for the element_to_be_clickable() which will automatically scroll the element within the Viewport and invoke click() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.buy-link.load-more-btn[value='LOAD MORE']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='buy-link load-more-btn' and @value='LOAD MORE']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Upvotes: -1

goalie1998
goalie1998

Reputation: 1442

Sort of a combination of the other two suggestions. If I understand Selenium correctly, it basically converts any find_element_* into a css selector, so having a space in the class name messes things up if you put both. This worked for me.

# Removes the Accept Cookies Banner
driver.find_element_by_css_selector("div.green-box-btn").click()
# Performs the click you want
driver.find_element_by_css_selector("input.buy-link").click()

Upvotes: 1

Ambrish Pathak
Ambrish Pathak

Reputation: 3968

find_element_by_class_name accepts only one class. You are passing multiple classes in it.

Try implementing it with one class only

driver.find_element_by_class_name("load-more-btn").click()

Upvotes: 3

r.burak
r.burak

Reputation: 544

driver.find_element_by_class_name("buy-link load-more-btn").click()

Please add click() to the end.

Upvotes: 0

Related Questions