2017-11-15 11 views
1

Seleniumで1つの入力に入力されたテキストを別の入力にミラーリングするテストを作成しようとしています。私はこのエラーを次のように続けています。Selenium + Pythonを使用して入力をテストするユーザー入力を別の入力にミラーリングする

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]"} 

これは私が現在使用しているコードです:

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">

+0

適切なHTMLコードを入力してください。 –

+0

質問にHTMLを追加しましたが、それが明確でない場合はお知らせください。 –

+0

あなたのコードでは絶対パスを使用しています。提供されたhtmlでは、誰もあなたのエラーについてあなたを助けることができません。 –

答えて

1

エラーは、構築xpathが意図した要素を識別することができないためであるそれをすべてNoSuchElementException: Message: no such elementを言います。参考のためのコードブロックは次のとおりです。

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

これは私の問題を解決しました、ありがとう! 'assert inputBoxTwoXpath.get_attribute(" value ")in userInput' –

0
  1. ここでは、のためのCSSセレクタです最初の入力:input[type="text"]。 2番目の場合:.textbox

  2. 要素の検索には、次のメソッドを使用します。driver.find_element_by_css_selector(your_css_selector)

試してください。ご質問がある場合は、コメントでお尋ねください。あなたが行うことができます提供HTMLで

1

、:

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) 
+0

これは助けてくれてありがとう! –

+0

@CalCorbinお手伝いします。この回答または他の誰かがあなたの問題を解決した場合は、それを受け入れたとマークしてください:stackoverflow.com/help/someone-answers –

関連する問題