Python / Selenium:如何在屏幕上自动按下“下一步”按钮?

时间:2018-10-01 10:25:46

标签: python selenium

我正在使用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"

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

帮助显示以下内容:

'Let''s Chat!'

因此,您可以尝试在循环中单击按钮,直到发生>>> 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]')