2016-12-24 57 views
1

我試圖選擇從dropdown.I所述元件中的一個有在檢索時我所使用的方法getAttribute() 12個元素:元件不可見:元素是當前不可見並且可以不被操縱

Select select = new Select(driver.findElement(By.xpath("//select[@id='dataset_downloadDataset_select']"))); 
    List<WebElement> options = select.getOptions(); 
    System.out.println(options.size()); 
    for (int i=1; i<=11; i++){ 
    System.out.println(options.get(i).getAttribute("value")); 

後檢索dropdowm的12個元素,我要選擇我試圖Actions/Javascriptexecutor them.For之一,但我得到用於Action方法元素不可見exception.The代碼:

WebElement mnuElement; 
    WebElement submnuElement; 

    mnuElement = driver.findElement(By.xpath("//input[starts-with(@data-activates,'sele')][@value='XXXXXXXXXXX']")); 
    submnuElement = driver.findElement(By.xpath("//*[@id='dataset_downloadDataset_select']/option[4]")); 
    Actions builder = new Actions(driver); 
    builder.moveToElement(mnuElement).perform(); 
    Thread.sleep(5000); 
    driver.findElement(By.xpath("//*[@id='dataset_downloadDataset_select']/option[4]")).click(); 

能anyo ne幫我解決這個問題。

+0

這是可能的,它需要一些時間下拉列表將被填充。你有沒有試過增加Selenium的時間來尋找元素? – derloopkat

+0

是的,我試過增加 –

+0

@vijay_m,'builder.moveToElement(mnuElement).perform();'並不意味着打開下拉菜單,所以你不能處理它的選項。嘗試點擊'mnuElement'之前點擊'submnuElement' – Andersson

回答

0

這取決於您使用的瀏覽器。 Chrome瀏覽器可以點擊選項,而無需擴展,而Firefox無法處理它。使用select從中進行選擇。您使用的選項要求單擊列表,然後選擇,但它是simplier去做那些

代碼的Firefox(也可能是每一個瀏覽器)

WebDriver driver;//then choosing browser 
element=driver.findElement(By.xpath("//select[@id='dataset_downloadDataset_select']"));//or whatever 
Select select=new Select(element); 

//To select what you want, this is selecting, nothing more 

select.selectByValue("Mainframe File 1");//or other 
+0

我使用Chrome瀏覽器。我嘗試了使用select.selectByValue(「從getattribute中檢索的值」),我收到了「找不到元素」。 –

+0

你可以發送該元素的HTML嗎? (檢查它並展開它的內容) – Myszsoda

+0