使用Selenium + Python将测试用户输入从一个输入镜像到另一输入 [英] Testing user input is mirrored from one input to another with Selenium + Python

查看:113
本文介绍了使用Selenium + Python将测试用户输入从一个输入镜像到另一输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Selenium编写一个测试,以确保输入到一个输入中的文本被镜像到另一个输入中.我一直在下面收到此错误.

I am trying to write a test in Selenium that ensures text entered into one input is mirrored in another input. I keep getting this error below.

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[2]/div/div/div/div[2]/div[1]/input[1]"}

这是我当前正在使用的代码:

This is the code I'm using currently:

class inputMirror(unittest.TestCase):
def setUp(self):
    self.driver = webdriver.Chrome()
    self.driver.get("https://foo.bar")

def test_mirror_input(self):
#Ensures text from the first input is mirrored into the second input box
    driver = self.driver
    userInput = "Howdy"
    inputBoxOneXpath = driver.find_element_by_xpath('/html/body/div/div[2]/div/div/div/div[2]/div[1]/input[1]')
    inputBoxTwoXpath = driver.find_element_by_xpath('/html/body/div/div[2]/div/div/div/div[2]/div[1]/input[2]')
    inputBoxOneXpath.clear()
    inputBoxOneXpath.send_keys(userInput)
    driver.implicitly_wait(10)
    assert inputBoxTwoXpath.text == userInput
    driver.close()

HTML代码:

<input type="text" placeholder="Enter text here..." value="">
<input class="textbox" placeholder="Enter text here..." value="" maxlength="80" required="true">

推荐答案

该错误说明了所有NoSuchElementException: Message: no such element的原因,这是因为您构建的xpath无法识别预期的元素.这是代码块供您参考:

The error says it all NoSuchElementException: Message: no such element which is because the xpath you constructed is unable to identify the intended element. Here is the code block for your reference :

driver = self.driver
userInput = "Howdy"
inputBoxOneXpath = driver.find_element_by_xpath("//input[@type='text' and @placeholder='Enter text here...']")
inputBoxOneXpath.clear()
inputBoxOneXpath.send_keys(userInput)
driver.implicitly_wait(10)
inputBoxTwoXpath = driver.find_element_by_xpath("//input[@class='textbox' and @placeholder='Enter text here...']")
assert inputBoxTwoXpath.get_attribute("value") in userInput
driver.close()

这篇关于使用Selenium + Python将测试用户输入从一个输入镜像到另一输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆