2015-11-27 56 views
0

對於html代碼,它顯示按鈕,但它是Input。selenium web驅動中的org.openqa.selenium.ElementNotVisibleException

<input id="cmdExport" type="submit" value="Save File" onmouseover="return MVstyle()" onmouseout="return MOstyle()" style="position: absolute; top: 50px; left: 20px; width: 100px; height: 30px; cursor: pointer;"> 

在我使用下面的代碼它不工作的硒webdriver。

WebElement saveButton = driver.findElement(By.xpath("input[@id='cmdExport']")); 
       saveButton.click(); 

此外,我試圖與

new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible("cmdExport"); 
       WebElement saveButton = driver.findElement(By.xpath("input[@id='cmdExport']")).sendKeys("TEXT"); 
       saveButton.click(); 

也試過下面的代碼不能正常工作,請幫助

WebElement saveButton = wait.until(ExpectedConditions 
     .presenceOfElementLocated(By.id("cmdExport"))); 

我試圖發佈完整htmlit的非常大的無法發佈。當該元件在DOM被加載,但是不可見的與被interaced發生

function MVstyle(){ 
       document.getElementById("cmdExport").style.cursor="pointer"; 
       document.getElementById("cmdExport").style.background="#66FF99"; 
       document.getElementById("cmdExport").style.border.radius="10px"; 
       document.getElementById("cmdExport").style.boxShadow="0px 0px 10px gray"; 
      }; 

      function MOstyle(){ 
       document.getElementById("cmdExport").style.background=""; 
       document.getElementById("cmdExport").style.boxShadow=""; 
      }; 


       //get parameters 
       var params = {}; 
       if (location.search) { 
        var parts = location.search.substring(1).split('&'); 
        for (var i = 0; i < parts.length; i++) { 
         var nv = parts[i].split('='); 
         if (!nv[0]) continue; 
         params[nv[0]] = nv[1]; 
        } 
       } 
       console.log('appId', params.appId); 
       console.log('objectId', params.objectId); 

       d = new Date(); 
       var dstring = d.valueOf();   
       var fileName = params.objectId+'_'+dstring; 

       //open apps 
       var app = qlik.openApp(params.appId, config); 

       app.getObject('QV01',params.objectId).then(function(vizModel) { 
        $('#cmdExport').show(); 
        $('#cmdExport').on('click', function() { 
         vizModel.exportData('CSV_C','/qHyperCubeDef',fileName,'A').then(function(reply) { 
          document.getElementById("fileLocation").href=reply.result.qUrl; 
          $("#fileLocation").show(); 
         }); 
        }); 
       }); 
      }); 
    </script> 
    <style> 
      article.qvobject 
      { 
       position:absolute; 
       overflow: hidden; 
       padding: 10px; 
      } 
    </style> 

    <body style="overflow:auto" class=""> 
    <form style="position: absolute; top: 10px; left: 20px;" class="ng-pristine ng-valid"> 
    </form> 

    <input id="cmdExport" type="submit" value="Save File" onmouseover="return MVstyle()" onmouseout="return MOstyle()" style="position: absolute; top: 50px; left: 20px; width: 100px; height: 30px; cursor: pointer;" class="xh-highlight"> 
    <a id="fileLocation" href="/tempcontent/e1d54811-d730-43d9-8114-6580793f6b1b/4003a0a9-bb23-4dc9-8568-9351a3770233.csv?serverNodeId=fd4d9fdf-d8be-4d1b-b176-8a8c3ac6bbe8" style="" ;="">Download Link</a> 
    <div id="QV01" style="position: absolute; top: 30px; left: 180px; width: 600px; height: 400px;" class="qvobject"><div class="qv-object-wrapper ng-scope ng-isolate-scope" model="model" options="options"> 
+0

在那裏有兩個與第一個按鈕相關的元素沒有顯示鼠標懸停和其他顯示時,我將鼠標懸停在按鈕上。懸停鼠標上顯示的元素正在使用javascript執行實際功能,直到我將鼠標懸停在按鈕上時,該元素纔可見。直接點擊該元素即爲ElementNotVisibleException。這是問題,我怎麼才能找到第二個元素。 –

回答

0

org.openqa.selenium.ElementNotVisibleException。在你的情況下,最可能的情況可能是,具有相同編號的多個元素cmdExport。所以Selenium從DOM獲取第一個具有相同ID(隱藏)的元素,並且針對它觸發該操作。

嘗試找到爲路徑定位器返回的元素數。

List<WebElement> saveButtons = driver.findElements(By.xpath("//input[@id='cmdExport']")); 

上面的代碼將返回所有匹配的XPath //input[@id='cmdExport']

如果列表大小超過一個,這意味着你具有給定XPath匹配多個網絡元素的元素。如果是這樣,你必須更加唯一地編寫xpath來定位所需的元素。

0

方法1:使用操作類:

 WebElement saveButton = driver.findElement(By.xpath("input[@id='cmdExport']")); 

     Actions action = new Actions(driver); 
     action.click(saveButton).perform(); 

選項2:插入JavaScript

 ((JavascriptExecutor)driver).executeScript("arguments[0].click();", saveButton); 
+0

不工作,相同的錯誤 –

0

下面的代碼應該做的伎倆:

WebElement hoverElement = driver.findElement(By.xpath("input[@id='cmdExport']")); 
Actions builder = new Actions(driver); 
builder.moveToElement(hoverElement).perform(); 
By locator = By.id("fileLocation"); 
driver.click(locator); 

就會找到元素,將鼠標懸停在上面,可以使「a」標籤可見並點擊「a」標籤。

我希望這會有所幫助。

+0

我找到xpath使用xpath助手,但不工作 –

+0

如果您在這裏發佈相關的HTML,我將能夠爲您提供幫助。 –

+0
相關問題