2012-12-05 25 views
0

我有一個SeleniumElement代表頁面上的幾個元素,我想過濾它只包含":visible"元素。過濾現有的SeleniumElement

我可能會改進原來的構造函數只包含一個elemnent,但我問我是否可以做一些不同的事情:給定一個特定的元素,創建一個新的元素,篩選此元素的選擇器。

事情是這樣的:

public static SeleniumElement visible(SeleniumElement element) { 
    // Locator.filter() doesn't really exist, the next line won't compile 
    By locator = element.getElementLocator().filter(":visible"); 
    return new SeleniumElement(element.getName(), locator, element.getPage()); 
} 

visible(myButton).click(); 

// Calling myButton.click() fails because there are multiple elements that match 
// the selector. However, only one of them is visible right now 
+0

對不起,我是用內部包裹我們有硒並沒有注意到,其實包裝,我認爲這是硒的代碼,我不能編輯(硒有沒有SeleniumElement類!) – ripper234

回答

1
如果要過濾通過自己的知名度的元素,然後使用CSS選擇器將是一個不錯的選擇

我已經添加了兩個CSS屬性顯示和可見性。因此,您可以使用任何一種屬性/屬性來查找目標元素。 (C#代碼)

IWebElement Query = driver.FindElement(By.XPath("Element Xpath")).FindElement(By.CssSelector("[@style='display: block;Visibility: hidden']")); 

我希望這有助於。 All the Best :-)

+0

對不起,我使用的是Selenium的內部包裝,並沒有注意到實際上是一個包裝,我認爲這是Selenium代碼,我不能編輯(Selenium沒有SeleniumElement類!) – ripper234

0

或者您可以執行基於該元素的點擊是否可見。

if (element.Visible) 
    { 
     element.click(); 
    }