我正在尝试在Selenium中编写一个测试,确保输入到一个输入的文本在另一个输入中被镜像。我在下面不断收到此错误。
http://docs.marklogic.com/guide/search-dev/relevance#id_68032
这是我目前正在使用的代码:
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]"}
HTML code:
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()
答案 0 :(得分:1)
错误说明全部为NoSuchElementException: Message: no such element
,这是因为您构建的xpath
无法识别目标元素。以下是供您参考的代码块:
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()
答案 1 :(得分:0)
使用提供的html,你可以这样做:
userInput = "Howdy"
driver.find_element_by_xpath("//input[@type='text' and @placeholder='Enter text here...']").send_keys(userInput)
driver.find_element_by_xpath("//input[@class='textbox' and @placeholder='Enter text here...']").send_keys(userInput)