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



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?

Answer

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
    


Source: stackoverflow