I have a problem to find element "Next Button" and click that with Selenium in Python within the webpage
Code trials:
try:
driver.find_element(By.CSS_SELECTOR, ".artdeco-pagination__button--next").click()
except NoSuchElementException:
print("No Next button, skipped.")
Any help?
The Next element on the webpage is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.get("https://www.linkedin.com/search/results/companies/?origin=SWITCH_SEARCH_VERTICAL&sid=ie%3B%5C")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Next'] li-icon.artdeco-button__icon +span.artdeco-button__text))).click()
Using XPATH:
driver.get("https://www.linkedin.com/search/results/companies/?origin=SWITCH_SEARCH_VERTICAL&sid=ie%3B%5C")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='artdeco-button__text' and text()='Next']"))).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