2017-02-24 104 views
0

我對Selenium和C#相當陌生,需要一些幫助。我一直在關注一些在線教程來創建一些基於硒的測試,但現在我試圖將它們應用到不同的站點。OpenQA.Selenium.ElementNotVisibleException:元素不可見Selenium C#

我的問題是,有一個單選按鈕,我不能選擇。我得到了

error: OpenQA.Selenium.ElementNotVisibleException

我看了很多文章,並嘗試了幾件事,但我無法得到它的工作。我不確定這是否是由於我的代碼無法正常工作,或者我需要嘗試其他方法。

我使用的網站是:url
輸入郵政編碼,電子郵件地址,然後點擊下一個你需要選擇一個燃料類型,但我找不到任何元素的選擇後。

我已經嘗試使用IJavaScriptExecutor,添加一個等待,並嘗試通過Id和xpath查找,但似乎沒有任何工作。我也嘗試最大化瀏覽器

這裏是我的代碼:

namespace TestAutomation 
{ 
    class SelectFuelObject 
    { 
    public SelectFuelObject() 
    { 
     PageFactory.InitElements(PropertiesCollection.driver, this); 
    } 

    [FindsBy(How = How.Id, Using = "dualFuel")] 
    public IWebElement btnFuelType { get; set; } 

    public void FuelType() 
    { 
     btnFuelType.ClickOnInvisibleElement(); 
    } 
    } 
} 

Javascript類

namespace TestAutomation 
{ 
    public static class HiddenElements 
    { 
     public static void ClickOnInvisibleElement(this IWebElement element) 
     { 
      ((IJavaScriptExecutor)PropertiesCollection.driver).ExecuteScript("arguments[0].hidden = false;", element); 
      element.Click(); 
     } 
    } 
} 

PropertiesCollection類

class PropertiesCollection 
    { 
     //Auto-Implement Property 
     public static IWebDriver driver { get; set; } 
    } 
} 

這是我想它等待剛導致分配時間後超時

WebDriverWait wait = new WebDriverWait(PropertiesCollection.driver, TimeSpan.FromSeconds(30)); 
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("DualFuel"))); 

請任何人都可以幫我嗎?

+0

您可能試圖點擊錯誤的元素,看起來像單選按鈕,但隱藏。請按照[如何提問](http://stackoverflow.com/help/how-to-ask)指南中的單選按鈕和周圍的'html'指導,直接在問題中發佈相關的'html'點擊。 – mrfreester

+0

有了硒,你可以使用xpath,所以只需在開發者模式下使用chrome,突出顯示單選按鈕,以便顯示與之相關的html,並右鍵單擊html獲取xpath – EpicKip

回答

0

inputid「DualFuel」不是可見的輸入,但它包含在我認爲您嘗試點擊的內容中。

試試這個:

[FindsBy(How = How.CssSelector, Using = ".opt.dual")] 
public IWebElement btnFuelType { get; set; } 

這將讓包含您輸入的div。您也可以嘗試單擊label,並使用for屬性「dualFuel」

然後,您只需單擊帶有btnFuelType.click()的元素即可。如果您遇到不可點擊或不存在的錯誤,請在評論中告訴我。在這個或大多數情況下,不應有任何理由使用IJavaScriptExecutor

+0

非常感謝。這很有效 –

+0

@RyanHoward很高興聽到它:)如果你覺得它有幫助,不要忘記註冊並接受答案。祝你好運! – mrfreester

相關問題