2017-06-18 115 views
0

我試圖點擊一個單選按鈕,唯一的問題是我不斷收到錯誤'元素不可見'。我曾在這個網站上看過類似的問題,並嘗試了幾件事情,但沒有一件似乎有效。如何使元素可見,以便我可以點擊它?OpenQA.Selenium.ElementNotVisibleException:元素不可見c#硒

當代碼達到'allships.Click();'發生錯誤。我的頁面對象類:

public class MatchShipmentsToLocationsPage 
    { 
     //instance of the IWebDriver interface 
     private IWebDriver _driver; 

     //fields that are used to find the needed HTML elements for our tests to pass 
     private By lineItemTableLocator = By.Id("lineItemTable"); 
     private By locationSearchInputBoxLocator = By.Id("locationSearch"); 
     private By groupSearchLocationSearchLocator = By.CssSelector(".input-group.search.location-search"); 

     private By applyLocationForLocator = By.Id("applyLocation"); 

     private By justThisInvoiceRadioButtonLocator = By.Id("rbThisInvoice"); 

     //constructor, constructs the the page 
     public MatchShipmentsToLocationsPage(IWebDriver driver) 
     { 
      this._driver = driver; 
     } 

public MatchShipmentsToLocationsPage selectAllFutureShipmentsRadioButton() 
     { 

      var applyOptions = _driver.FindElement(applyLocationForLocator); 

      var allships = applyOptions.FindElements(By.Name("locationGroup"))[1]; 

      allships.Click(); 
      Thread.Sleep(5000); 
      return this; 
     } 

    } 

的HTML:

<div id="applyLocation" class="panel-subsection"> 
        <div class="panel-subsection-header">Allow for</div> 
        <div class="radio-button-section"> 
         <div class="container radiocontainer"> 
::before 
          <input type="radio" checked="checked" name="locationGroup" id="rbThisInvoice"> 
          <label for="rbThisInvoice"> 
           <span class="radio inline radio-span radio-label">Just this invoice</span> 
          </label> 
::after 
         </div> 

         <div class="container radiocontainer"> 
::before 
          <input type="radio" name="locationGroup" id="rbAllShipments"> 
          <label for="rbAllShipments"> 
           <span class="radio inline radio-span radio-label"> 
      ::before 
       All Future Shipments</span> 
          </label> 
      ::after 
         </div> 
        </div> 
       </div> 
+0

如果你想出了一個解決方案,它張貼作爲一個答案並接受它,所以問題被標記爲已解決。 – JeffC

+0

謝謝,只是做了@JeffC –

回答

0

我解決它:)我通過JavaScript點擊的元素:

public MatchShipmentsToLocationsPage selectJustThisInvoiceRadioButton() 
     { 

      var applyOptions = _driver.FindElement(applyLocationForLocator); 
      //var justThisInvoiceRadioButton = applyOptions.FindElement(By.Id()) 
      //_driver.FindElement(justThisInvoiceRadioButtonLocator).Click(); 

      //var allFutureShipmentsRadioButtonLocator = applyOptions.FindElement(By.Id("rbAllShipments")); 
      //allFutureShipmentsRadioButtonLocator.Click(); 

      var allships = applyOptions.FindElements(By.Name("locationGroup"))[1]; 

      IJavaScriptExecutor js = (IJavaScriptExecutor)_driver; 
      js.ExecuteScript("arguments[0].click()", allships); 


      // allships.Click(); 
      Thread.Sleep(5000); 
      return this; 
     } 
相關問題