2016-08-15 9 views
2

私はクリアしたい&与えられたURLの入力の値を変更します。使用したjsは何もせず、エラーも発生しません。なぜこれが起こっているのですか?それをどうやって修正しますか?javascriptで入力フィールドをクリアできないのはなぜですか?

@Test 
public void clearField() throws Exception { 
    String url = "https://sfbay.craigslist.org/search/pen/apa?hasPic=1&search_distance=25&" + 
      "postal=94014&nh=75&nh=80&min_price=1500&max_price=2500&bedrooms=1&bathrooms=1"; 
    //url = "https://sfbay.craigslist.org/search/pen/apa?housing_type=1";//Clear & set value works with this url. 
    browser.get(url); 

    WebElement element = browser.findElement(By.name("search_distance")); 
    String char_sequence = "10"; 

    //Clear the field 
    send_keys_v2(element, ""); 
    //Re write the field 
    send_keys_v2(element, char_sequence); 
} 

public void send_keys_v1(WebElement element, String char_sequence) { 
    ((JavascriptExecutor) browser).executeScript("arguments[0].value='" + 
      char_sequence + "';", element); 
} 

public void send_keys_v2(WebElement element, String char_sequence) { 
    ((JavascriptExecutor) browser).executeScript("arguments[0].setAttribute=('value', '" + 
      char_sequence + "');", element); 
} 

参考文献:Set value of input instead of sendKeys() - selenium webdriver nodejs

How can I consistently remove the default text from an input element with Selenium?

答えて

1

考えられる1つの答えは、クリアしようとするものの前に同じ名前の別の要素があることです。

複数の要素が見つかった場合は、最初に取得された要素があるかどうかを確認してください。

.searchInput[name*=search_distance]

:あなたのようなCSSセレクタを使用することができます別の方法として

1

セレンに使用してみてください:明確な

WebElement.clear()

のボイド()この要素がテキスト入力要素である場合には、この値 をクリアします。他の要素には影響しません。テキスト入力要素は、 のINPUT要素とTEXTAREA要素です。 More info here

関連する問題