2016-12-07 38 views
0

我試圖自動化一個網站,我可以創建一個列表。 通過在文本字段中輸入值創建列表,然後通過在文本字段外單擊鼠標來設置該值。 然後重複上一步,將更多元素輸入到列表中。 由於我們使用的是Internet Explorer,因此我在IE中使用了Selenium webdriver。 sendKeys方法非常慢,所以我想使用executecript方法。不能使用執行腳本方法與多個參數

我想這樣如下(它不工作),我想知道是否有人有建議。

的代碼片段:

//Here I declare an arry with the values for the list to be created 

String valores[] = {"18","25","60","71"}; 

//Here I get the WebElement where I'm going to enter the above values one by one 

WebElement searchField = driver.findElement(By.xpath("//label[text()='New value:']/parent::div//input[@type='text']")); 

JavascriptExecutor myExecutor6 = ((JavascriptExecutor) driver); 

//Now I’m trying to use a for cycle to enter the values one by one, I need to do this, because this is how the UI works  

for(int i=0; i<valores.length; i++){ 
     System.out.println(valores[i]); 
     Thread.sleep(3000); 
/*this part is not working, here what I’m trying to do is: I’m trying to write the value of array valores in position ‘i’ in the text field indicated by the WebElement searchField */ 
/*I have a javascript error, I do notunderstand what is wrong*/ 

     myExecutor6.executeScript("var valueToWrite = valores[i].toString();" 
+"arguments[0].value = valueToWrite ",searchField, valores[i]); 

     Thread.sleep(3000); 

//This step clicks somewhere else with the mouse to enter the value, 
     driver.findElement(By.xpath("//label[text()='Values:']")).click(); 
     Thread.sleep(3000); 
    } 
+4

什麼是錯誤? – Guy

+0

我改變了代碼,它仍然失敗,然後我在JavaScript中使用focus()方法,它工作。感謝您試圖提供幫助。 (int i = 0; i

回答

0

我已經改變了代碼,它仍然失敗,同樣的問題。看起來,在JavaScript中,把焦點放在元素上是不切實際的。這解決了問題 arguments [0] .focus();

感謝您的幫助。

新代碼:

 
public static void createList(WebDriver driver, String array[]) throws InterruptedException{

String[] values = array.clone(); String iterationNumber; JavascriptExecutor myExecutor = ((JavascriptExecutor) driver); for(int i=0; i<values.length; i++){ System.out.println(values[i]); Thread.sleep(1000); iterationNumber = values[i]; myExecutor.executeScript("arguments[0].focus();"+"arguments[0].value = arguments[1];",driver.findElement(By.xpath("//label[text()='New value:']/parent::div//input[@type='text']")), iterationNumber); Thread.sleep(1000); driver.findElement(By.xpath("//label[text()='Values:']")).click(); Thread.sleep(1000); driver.findElement(By.xpath("//label[text()='New value:']/parent::div/div/div[2]/button")).click(); Thread.sleep(1000); } }