Python/Selenium:如何在屏幕上自动按下按钮"Next"?



我正在使用python在Selenium上编写自动测试。 调用窗口后,屏幕上会显示工具提示。完成教程有 27 个步骤(27 个工具提示(。逐个显示的工具提示。每个工具提示上都有"下一步"按钮(每个步骤都有相同的 xpath(。 如何使硒点击"下一步"按钮,同时显示在屏幕上?

step1 = driver.find_element_by_xpath('/html/body/div[2]/div/button')
step1.click()
print "Step 1 = OK"
step2 = driver.find_element_by_xpath('/html/body/div[2]/div/button')
step2.click()
print "Step 2 = OK"

您可以使用 Selenium 中的预期条件功能来等待元素的可见性。

from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
wd_wait = WebDriverWait(driver, timeout)
confirm1 = wd_wait.until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[2]/div/button')))
confirm1.click()

帮助显示以下内容:

>>> help(driver.find_element_by_xpath)
Help on method find_element_by_xpath in module selenium.webdriver.remote.webdriver:
find_element_by_xpath(xpath) method of selenium.webdriver.firefox.webdriver.WebDriver instance
Finds an element by xpath.
:Args:
- xpath - The xpath locator of the element to find.
:Returns:
- WebElement - the element if it was found
:Raises:
- NoSuchElementException - if the element wasn't found
:Usage:
element = driver.find_element_by_xpath('//div/td[1]')

因此,您可以尝试循环单击该按钮,直到发生NoSuchElementException

最新更新