Home > Net >  How can I click the button and move to next page using selenium?
How can I click the button and move to next page using selenium?

Time:11-16

I am not able to click the button using selenium and move to the next page. I have tried the following commands:

driver.find_element_by_xpath(".//div[text()='Production']").click() 
driver.find_element_by_xpath('//*[@id="root"]/div/[1]/div/div/div[2]/button[2]').click 

I have added a screenshot of the html.

enter image description here

What am I doing wrong?

CodePudding user response:

I guess the pagination button is on the bottom of the page, out of the visible screen. If so, to click it you first have to scroll the page down, bring that element into the view and only then click it.
Please try this:

pager = wait.until(EC.presence_of_element_located((By.XPATH, "//div[text()='Production']")))
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(0.5)
actions.move_to_element(pager).perform()
time.sleep(0.5)
driver.find_element_by_xpath("//div[text()='Production']").click() 

CodePudding user response:

To click on the element with text as Production you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "button.nav-option.nav-option--main > div.section-title").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//button[@class='nav-option nav-option--main']/div[@class='section-title']//div[text()='Production']").click()
    

Ideally to click on the 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:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.nav-option.nav-option--main > div.section-title"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='nav-option nav-option--main']/div[@class='section-title']//div[text()='Production']"))).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
    
  • Related