[英]How can I click the button and move to next page using selenium?
要单击带有文本作为生产的元素,您可以使用以下任一定位器策略:
使用css_selector
:
driver.find_element(By.CSS_SELECTOR, "button.nav-option.nav-option--main > div.section-title").click()
使用xpath
:
driver.find_element(By.XPATH, "//button[@class='nav-option nav-option--main']/div[@class='section-title']//div[text()='Production']").click()
理想情况下,单击需要为element_to_be_clickable()
引发WebDriverWait的元素,您可以使用以下任一定位器策略:
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.nav-option.nav-option--main > div.section-title"))).click()
使用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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
我猜分页按钮在页面底部,在可见屏幕之外。 如果是这样,要单击它,您首先必须向下滚动页面,将该元素放入视图中,然后才单击它。
请试试这个:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.