简体   繁体   中英

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.

在此处输入图片说明

What am I doing wrong?

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

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() 

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